【问题标题】:Flexible way to show a lot of columns in datagrid在数据网格中显示大量列的灵活方式
【发布时间】:2016-06-23 17:07:44
【问题描述】:

我在 WPF DataGrid 中有很多包含订单的列,超过 80 个。它们可以根据视图选项菜单显示或隐藏。现在我正在单独做选项菜单,单独订购视图模型,OnAutoGeneratingColumn 事件中的列可见性和标题处理。所以我有 3 个不同的类(ViewOptions、OrdersViewModel、ViewOptionsViewModel)和事件处理程序中的很多逻辑。此外,有必要在添加/删除列的 4 个位置修改代码。

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

【问题讨论】:

    标签: c# wpf binding datagrid


    【解决方案1】:

    使用绑定,当我检查“显示属性1,列可见

    Xaml:

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

    c#代码:

      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));
        }
    

    【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-08
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多