WPF触发datagrid根据绑定隐藏/显示列

时间:2010-12-21 12:41:43

标签: wpf binding wpfdatagrid

我有一个这样的数据网格:

<DataGrid AutoGenerateColumns="False" CanUserDeleteRows="True"
          HorizontalScrollBarVisibility="Hidden" Margin="10,10,10,10" 
          VerticalScrollBarVisibility="Visible" 
          CanUserAddRows="False" ItemsSource="{Binding ListGestores}"
          ToolTip="Selecione uma linha e pressione DELETE para remover uma unidade.">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding TB_UNIDADE.DS_NOME_UNIDADE}" CanUserResize="False" Header="Setor" IsReadOnly="True" x:Name=""/>
        <DataGridTextColumn Binding="{Binding TB_UNIDADE.TB_UNIDADE2.DS_NOME_UNIDADE}" CanUserResize="False" Header="Unidade" IsReadOnly="True" x:Name=""/>
        <DataGridTextColumn Binding="{Binding TB_CONTATOS.DS_NOME}" CanUserResize="False" Header="Gestor" IsReadOnly="True" />
    </DataGrid.Columns>
</DataGrid>

问题是我需要验证2列绑定是否为null,如果它为null,我需要隐藏它,并更改列1的标题。

我知道我可以用触发器做到这一点,但究竟是怎么回事? 我开始时:

<DataGrid.Triggers>
    <DataTrigger Binding="{Binding TB_UNIDADE.TB_UNIDADE2}" Value="{x:Null}">
        <Setter Property="" Value="" />
    </DataTrigger>
</DataGrid.Triggers>

但是我不知道要放什么东西或什么东西!

帮帮我=]

1 个答案:

答案 0 :(得分:2)

实施例

使用以下ViewModel:

public class ViewModel : INotifyPropertyChanged
{
    public String Data1 { get; set; }
    public String Data2 { get; set; }
    public String Data3 { get; set; }
}

您可以使用以下示例XAML:

<Grid.Resources>
    <local:DataGridColumnVisibilityConverter x:Key="DataGridColumnVisibilityConverter" />
</Grid.Resources>
<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Data1}"
                            Visibility="{Binding, Converter={StaticResource DataGridColumnVisibilityConverter}, ConverterParameter=Data1}" />
        <DataGridTextColumn Binding="{Binding Data2}"
                            Visibility="{Binding, Converter={StaticResource DataGridColumnVisibilityConverter}, ConverterParameter=Data2}" />
        <DataGridTextColumn Binding="{Binding Data3}"
                            Visibility="{Binding, Converter={StaticResource DataGridColumnVisibilityConverter}, ConverterParameter=Data3}" />
    </DataGrid.Columns>
</DataGrid>

转换器:

public class DataGridColumnVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ViewModel viewModel = (ViewModel)value;

        switch (parameter.ToString().ToLower())
        {
            case "data1":
                return (!String.IsNullOrEmpty(viewModel.Data1)) ? Visibility.Visible : Visibility.Collapsed;
            case "data2":
                return (!String.IsNullOrEmpty(viewModel.Data2)) ? Visibility.Visible : Visibility.Collapsed;
            case "data3":
                return (!String.IsNullOrEmpty(viewModel.Data3)) ? Visibility.Visible : Visibility.Collapsed;
        }

        return Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}