数据网格上的 WPF 触发器根据绑定隐藏/显示列

发布于 2024-10-08 15:02:28 字数 1276 浏览 3 评论 0原文

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

<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列绑定是否为空,如果为空,我需要隐藏它,并更改第1列的标题。

我知道我可以使用触发器来做到这一点,但具体如何??? 我一开始是:

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

但我不知道该放什么二传手或什么东西!

帮助我=]

I have a data grid like this:

<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>

The problem is that i need to verify if the 2 column binding is null, and if it is null, i need to Hide it, and Change the Header of the column 1.

I know that i can do that with Triggers, but how exactly???
I started with:

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

But i don't know what setter or whatever to put !

Help me =]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2024-10-15 15:02:28

示例:

对于以下 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();
    }
}

Example:

With the following ViewModel:

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

You can use following sample 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>

And converter:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文