显示/隐藏DataGrid列XAML [英] Show/Hide DataGrid Columns XAML

查看:60
本文介绍了显示/隐藏DataGrid列XAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用控件构建一个DataGrid,该控件将允许用户显示/隐藏列.我的DataGrid将有40列之类的内容,并非始终都有必要.我已经能够使用使用GridView的ListView做到这一点.这是代码:

I am trying to build a DataGrid with controls that will allow a user to Show/Hide the columns. My DataGrid will have something like 40 columns, and not all may be necessary all of the time. I have been able to do this exact thing with a ListView that uses a GridView. Here is the code:

<DataGrid Name="MyDataGrid" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding ReportOrderingCustomersForSalesRepCollection}" Style="{DynamicResource styleDataGrid}" HeadersVisibility="All" AutoGenerateColumns="False" RowHeaderWidth="0" RowHeight="25">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Customer #" Binding="{Binding CustomerNumber}" Width="90" Visibility="{Binding ElementName=Visibility_Txt,Path=Text,Mode=OneWay}"/>
        <DataGridTextColumn Header="Customer Name" Binding="{Binding CustomerName}" Width="125" />
        <DataGridTemplateColumn Header="Email" CellTemplate="{StaticResource Email}" Width="150" />
    </DataGrid.Columns>
</DataGrid>
<!-- text box -->
<TextBox Name="Visiblility_Txt">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ColumnVisibilityCheck,Path=IsChecked}" Value="False">
                    <Setter Property="Text" Value="Collapsed" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=ColumnVisibilityCheck,Path=IsChecked}" Value="True">
                    <Setter Property="Text" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
<!-- checkbox -->
<CheckBox Content="Show/Hide Customer Number" Name="ColumnVisibilityCheck" IsChecked="True" />

所以我有一个DataGrid设置.将第一个DataGridTextColumn上的Visibility属性设置为对"Visibility_Txt"的文本属性的绑定.该文本框上的文本将设置为折叠"或可见",具体取决于是否选中ColumnVisibilityCheck.

So I have a DataGrid setup. The Visibility Property on the first DataGridTextColumn is set as a binding to the text property of "Visibility_Txt". The text on that text box will be set to either Collapsed or Visible depending if the ColumnVisibilityCheck is checked or not.

就像我说的那样,这适用于列表视图,为什么不适用于DataGrid?

Like i said, this works with a listview, Why won't this work with a DataGrid?

推荐答案

能够找到更多有关此的信息.该链接有一个很好的答案/解释. datagridtextcolumn-visibility-binding

Was able to find some more information on this. This link has a good answer/explanation. datagridtextcolumn-visibility-binding

事实证明,DataGrid的列未出现在DataGrid的可视树中.

It turns out that the columns of a DataGrid do not appear in the visual tree of a DataGrid.

但是答案是在可见性绑定中使用x:reference和BooleanToVisibilityConverter:

But the answer is to use x:reference in the visibility binding, and a BooleanToVisibilityConverter:

<DataGridTextColumn Header="Customer #" x:Name="CustNum_Col" Visibility="{Binding Source={x:Reference VisibilityCheck}, Path=IsChecked,Converter={StaticResource ObjectToVisibilityConverter}}" />

Visual Studio将在绑定下方显示弯曲的线条,说明未将对象设置为对象实例".但这似乎仍然有效.

Visual Studio will show the squiggly line under the binding saying that "object not set to instance of an object" but it this still appears to work.

这篇关于显示/隐藏DataGrid列XAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆