WPF之使用“依赖属性”实现动态添加DataGrid“头”和“列”(MVVM模式)

1、首先自定义控件——DataGridEx

添加依赖属性:DynamicBindHeadersCols

    /// <summary>
    /// DataGridEx.xaml 的交互逻辑
    /// </summary>
    public partial class DataGridEx : DataGrid
    {
        /// <summary>
        /// 动态绑定DataGrid头和列
        /// </summary>
        public DataGrid DynamicBindHeadersCols
        {
            get { return (DataGrid)GetValue(DynamicBindHeadersColsProperty); }
            set { SetValue(DynamicBindHeadersColsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for dataGrid.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DynamicBindHeadersColsProperty =
            DependencyProperty.Register("DynamicBindHeadersCols", typeof(DataGrid), typeof(DataGridEx), new PropertyMetadata((s, e) =>
            {
                var dp = s as DataGridEx;
                foreach (var item in (e.NewValue as DataGrid).Columns)
                {
                    dp.Columns.Add(new DataGridTextColumn()
                    {
                        Header = item.Header,
                        Binding = item.ClipboardContentBinding,
                        Width = item.Width,
                        Visibility = item.Visibility,
                        IsReadOnly = item.IsReadOnly,
                    });
                }
            }));
    }

2、前台Xaml代码:

    <Grid>
        <controlex:DataGridEx ItemsSource="{Binding TestValues}" Style="{StaticResource DataGridExStyle}" DynamicBindHeadersCols="{Binding DataGrids}" />
    </Grid>

3、ViewModel代码:

    public class MainViewModel : INotify
    {
        public object TestValues { get; set; }
        public MainViewModel()
        {
            DataGridLength length = new DataGridLength(1, DataGridLengthUnitType.Star);
            dataGrids.Columns.Add(new DataGridTextColumn() { Header = "姓名", Binding = new Binding("Name"), Width = length, IsReadOnly = true });
            dataGrids.Columns.Add(new DataGridTextColumn() { Header = "年龄", Binding = new Binding("Age"), Width = length, IsReadOnly = false });
            TestValues = new[] {
                new { Name = "张三", Age = 23 },
                new { Name = "李四", Age = 24 },
                new { Name = "王五", Age = 25 },
            };
        }

        private DataGrid dataGrids = new DataGrid();

        public DataGrid DataGrids
        {
            get { return dataGrids; }
            set
            {
                dataGrids = value;
                RaisePropertyChanged();
            }
        }
    }

4、实现效果:

 

 不到之处请大家多多指正,谢谢!

MVVM是一种软件架构模式,用于将用户界面的逻辑与数据分离,以便更好地管理和维护代码。在WPF中,可以使用MVVM模式实现DataGrid动态和编辑器。 首先,我们可以创建一个ViewModel类,该类将持有DataGrid的数据和信息。我们可以使用ObservableCollection<T>来存储数据,这样当数据发生变化时,DataGrid自动更新。 然后,我们可以在ViewModel中定义一个命令,用于处理用户对DataGrid中的数据进行编辑的操作。当用户点击编辑按钮时,命令会被调用,并传递要编辑的数据作为参数。 接下来,我们可以在View中使用DataGrid来展示ViewModel中的数据。通过绑定DataGrid的ItemsSource属性到ViewModel中的数据集合,可以实现动态的效果。当ViewModel中的数据发生变化时,DataGrid自动刷新。 在DataGrid定义中,我们可以使用DataGridTemplateColumn来定义自定义的编辑器。通过绑定该的CellTemplate和CellEditingTemplate属性到ViewModel中的编辑器,在用户编辑数据时,可以使用自定义的编辑器来展示和保存数据。 最后,我们需要将View与ViewModel进行绑定,以实现数据的双向同步。可以使用DataBinding来将ViewModel中的属性与View中的控件进行绑定,这样当属性发生变化时,控件自动更新,并且当用户对控件进行操作时,属性也会相应地更新。 总而言之,使用MVVM模式可以将DataGrid动态和编辑器实现得更加灵活和可维护。通过将数据和逻辑分离,我们可以更好地组织代码,并实现更好的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值