DataGrid中的WPF可见性绑定列

时间:2018-02-18 00:41:11

标签: wpf binding datagrid visibility frameworkelement

我有一个DataGrid,我试图控制每列的​​可见性。当我将列的源绑定到实际的ViewModel时,一切正常,但我读过该列应该绑定到一个框架元素,我不完全理解(因为它不起作用)。

我以不同的方式绑定每一列。一个直接到ViewModel,它的工作原理。我绑定到FrameworkElement的另一列不起作用。我从一个更大的代码块复制了这个例子,但我认为我得到了一切相关的东西。

有人可以告诉我应该怎么做。任何解释,以帮助我理解也将不胜感激。

<Page x:Class="gobo2018.View.TestPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:gobo2018.View"
      xmlns:ViewModel="clr-namespace:gobo2018.ViewModel"

      Title="TestPage">

    <Page.Resources>
        <ViewModel:ActiveJobViewModel x:Key="ActiveJobViewModel"/>

        <Style TargetType="{x:Type TextBlock}" x:Key="WrapText">
            <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
    </Page.Resources>

    <Grid Grid.Row="9" HorizontalAlignment="Stretch" DataContext="{Binding Mode=OneWay, Source={StaticResource ActiveJobViewModel}}"  >
        <Grid.Resources>
            <FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/>
        </Grid.Resources>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="0"/>
            <RowDefinition MaxHeight="610"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>


        <StackPanel Grid.Row="0" Orientation="Vertical" Style="{StaticResource GlobalStackPanelStyle}">

            <StackPanel Orientation="Horizontal" VerticalAlignment="Top" Background="Black">

                <CheckBox VerticalAlignment="Center" IsChecked="{Binding EditVisibility, UpdateSourceTrigger=PropertyChanged}" IsTabStop="False" Margin=" 3 0 0 0 "/>
                <Label Content="Edit/Delete" Margin=" 0 0 13 0 "  HorizontalAlignment="Right" VerticalAlignment="Center"/>

                <CheckBox VerticalAlignment="Center" IsChecked="{Binding DateVisibility, UpdateSourceTrigger=PropertyChanged}" IsTabStop="False" Margin=" 3 0 0 0 "/>
                <Label Content="Date" Margin=" 0 0 13 0 "  HorizontalAlignment="Right" VerticalAlignment="Center"/>

            </StackPanel>
        </StackPanel>



        <DataGrid Grid.Row="2" x:Name="EstimateGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" 
                              RowDetailsVisibilityMode="VisibleWhenSelected"  MaxHeight="600" Width="Auto" HorizontalAlignment="Left"
                              SelectionUnit="FullRow"  
                              SelectionMode="Single" 
                              ItemsSource="{Binding EstimateSortView, UpdateSourceTrigger=PropertyChanged}"
                              CanUserAddRows="False"
                              IsReadOnly="True"
                              SelectedItem="{Binding EstimateSelectedItem}"
                              SelectedIndex="-1"
                              SelectedValue ="{Binding EstimateGridSelectedValue}">

            <DataGrid.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"  Color="Yellow"/>
            </DataGrid.Resources>


            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Edit" 
                                                    Visibility="{Binding EditVisibility, Converter={StaticResource booleanToVisibility}, UpdateSourceTrigger=PropertyChanged, Source={StaticResource ProxyElement}}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>

                            <Button Content="Edit Row" Foreground="DarkRed" FontSize="10" VerticalAlignment="Center" Padding="3"
                                                Command="{Binding Path=DataContext.ShowEstimatingUserControlCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                                                CommandParameter="Edit"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>

                <DataGridTemplateColumn Header="Delete"
                                                    Visibility="{Binding EditVisibility, Converter={StaticResource booleanToVisibility}, UpdateSourceTrigger=PropertyChanged, Source={StaticResource ActiveJobViewModel}}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>

                            <Button Content="Delete" Foreground="DarkRed" FontSize="10"  VerticalAlignment="Center" Padding="3"
                                                Command="{Binding Path=DataContext.EstimateWindowDeleteDatagridRowCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                                                CommandParameter="Delete"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>


            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Page>

1 个答案:

答案 0 :(得分:0)

由于Visibility不是DependencyProperty,它无法绑定。因此,而不是使用默认的Visibility创建一个新的附加属性

public class VisibilityAttachedProperty
{
    public static bool GetVisibility(DependencyObject obj)
    {
        return (bool)obj.GetValue(VisibilityProperty);
    }
    public static void SetVisibility(DependencyObject obj, bool value)
    {
        obj.SetValue(VisibilityProperty, value);
    }
    public static readonly DependencyProperty VisibilityProperty =
    DependencyProperty.RegisterAttached("Visibility", typeof(bool), typeof(VisibilityAttachedProperty), new PropertyMetadata(true, OnVisibilityChanged));
    private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue)
        {
            (d as DataGridColumn).Visibility = Visibility.Visible;
        }
        else
        {
            (d as DataGridColumn).Visibility = Visibility.Collapsed;
        }
    }
}

&#13;

<DataGridTemplateColumn Header="Edit"  local:VisibilityAttachedProperty.Visibility="{Binding EditVisibility}">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>

                <Button Content="Edit Row" Foreground="DarkRed" FontSize="10" VerticalAlignment="Center" Padding="3"
                                                Command="{Binding Path=DataContext.ShowEstimatingUserControlCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
                                                CommandParameter="Edit"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>