Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在datagrid中显示大量列的灵活方式_C#_Wpf_Binding_Datagrid - Fatal编程技术网

C# 在datagrid中显示大量列的灵活方式

C# 在datagrid中显示大量列的灵活方式,c#,wpf,binding,datagrid,C#,Wpf,Binding,Datagrid,我在WPF DataGrid中有很多列包含订单,超过80个。它们可以是可见的,也可以是隐藏的,具体取决于“视图选项”菜单。现在我分别处理选项菜单、订单视图模型、列可见性和事件中的标题处理。因此,我有3个不同的类(ViewOptions、OrdersViewModel、ViewOptions ViewModel)和事件处理程序中的许多逻辑。此外,还需要在添加/删除列的4个位置修改代码 是否有更好的方法将菜单标题绑定到列标题,以及将列可见性(DataGrid)绑定到菜单(ViewOptions Vi

我在WPF DataGrid中有很多列包含订单,超过80个。它们可以是可见的,也可以是隐藏的,具体取决于“视图选项”菜单。现在我分别处理选项菜单、订单视图模型、列可见性和事件中的标题处理。因此,我有3个不同的类(ViewOptions、OrdersViewModel、ViewOptions ViewModel)和事件处理程序中的许多逻辑。此外,还需要在添加/删除列的4个位置修改代码

是否有更好的方法将菜单标题绑定到列标题,以及将列可见性(DataGrid)绑定到菜单(ViewOptions ViewModel)中的复选框


使用绑定,当我选中“显示属性1”时,列可见

Xaml:


c#代码:

公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表=新列表();
对于(int i=0;i<10;i++)
{
TestData项=新的TestData();
item.Property1=“Property1”+i.ToString();
item.Property2=“Property2”+i.ToString();
item.Property3=“Property3”+i.ToString();
列表。添加(项目);
}
this.DataContext=list;
}
}
公共类测试数据
{
公共字符串属性1{get;set;}
公共字符串属性2{get;set;}
公共字符串属性3{get;set;}
}
公共类Bool2Visibility:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
布尔标志=假;
if(值!=null)
{
flag=System.Convert.ToBoolean(值);
}
返回标志?可见性。可见:可见性。折叠;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
返回null;
}
}
公共类BindingProxy:Freezable
{
#Freezable的区域覆盖
受保护的重写Freezable CreateInstanceCore()
{
返回新的BindingProxy();
}
#端区
公共对象数据
{
获取{return(object)GetValue(DataProperty);}
set{SetValue(DataProperty,value);}
}
//使用DependencyProperty作为数据的后备存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty DataProperty=
Register(“数据”、typeof(对象)、typeof(BindingProxy)、新UIPropertyMetadata(null));
}
<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:test="clr-namespace:WpfApplication6"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <test:Bool2Visibility x:Key="bool2Visibility"/>
        <test:BindingProxy x:Key="bpProperty1"/>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal">
            <CheckBox  Content="Display Property1" IsChecked="{Binding Source={StaticResource  bpProperty1},Path=Data,Mode=OneWayToSource}"/>
        </StackPanel>
        <DataGrid Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="property1" Binding="{Binding Property1}" Visibility="{Binding Source={StaticResource  bpProperty1},Path=Data,Converter={StaticResource bool2Visibility}}"/>
                <DataGridTextColumn Header="property2" Binding="{Binding Property2}"/>
                <DataGridTextColumn Header="property3" Binding="{Binding Property3}"/> 
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
    </Window>
  public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<TestData> list = new List<TestData>();
            for (int i = 0; i < 10; i++)
            {
                TestData item = new TestData();
                item.Property1 = "property1" + i.ToString();
                item.Property2 = "property2" + i.ToString();
                item.Property3 = "property3" + i.ToString(); 
                list.Add(item);
            }
            this.DataContext = list; 
        }
    }

    public class TestData
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; } 

    }

    public class Bool2Visibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool flag = false;
            if (value != null)
            {
                flag = System.Convert.ToBoolean(value);
            }
            return flag ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }


    public class BindingProxy : Freezable
    {
        #region Overrides of Freezable

        protected override Freezable CreateInstanceCore()
        {
            return new BindingProxy();
        }

        #endregion

        public object Data
        {
            get { return (object)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
    }