WPF MvvM DataGrid 动态列 [英] WPF MvvM DataGrid Dynamic Columns

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

问题描述

我正在搜索如何以 MvvM 方式从 ToolKit 动态创建 DataGrid 的列.但看起来不可能!

I am searching about how to create the columns of the DataGrid from the ToolKit dynamic in MvvM way. But looks like it is impossible !

有没有人必须做同样的事情?

Is there some one that had to do the samething ?

不需要创建来自 DataGrid 的用户控件或其他控件,我只想将网格的 de ItemSource 设置为我的自定义对象,并且在某些时候我想在基于运行时动态定义网格的列在对象的种类中.

there is no need to create a usercontrol or another control that comes from DataGrid, I just want to set de ItemSource of the grid to my custom object and in some point I want to define the columns of the grid in runtime dynamic based in the kind of the object.

这可能吗?

干杯

推荐答案

我将在开头说这可能不是最好的解决方案,并且在某些情况下可能不起作用,但你可以给它一个尝试看看它是否适合你想要的.我只是把它搅起来,所以它可能有一些错误.它仍然会涉及一些代码,但它会使您的模型无法了解您的视图.

I'm going to preface this by saying it maybe isn't the best solution to be doing and may not work in some situations but you can give it a try and see if it will work for what you want. I just whipped this up so it may have some bugs in it. Its still going to involve some code, but it keeps your model from knowing about you view.

您需要做的是创建一个扩展属性,允许您绑定 DataGrid 上的 Columns 属性.这是我拼凑的一个例子.

What you need to do is create an extension property that will allow you to bind the Columns property on the DataGrid. Here is an example that I put together.

using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;

public static class DataGridExtension
{
    public static ObservableCollection<DataGridColumn> GetColumns(DependencyObject obj)
    {
        return (ObservableCollection<DataGridColumn>)obj.GetValue(ColumnsProperty);
    }

    public static void SetColumns(DependencyObject obj, ObservableCollection<DataGridColumn> value)
    {
        obj.SetValue(ColumnsProperty, value);
    }

    public static readonly DependencyProperty ColumnsProperty =  
           DependencyProperty.RegisterAttached("Columns",
           typeof(ObservableCollection<DataGridColumn>),
           typeof(DataGridExtension),
           new UIPropertyMetadata (new ObservableCollection<DataGridColumn>(),
           OnDataGridColumnsPropertyChanged));


    private static void OnDataGridColumnsPropertyChanged(
           DependencyObject d,
           DependencyPropertyChangedEventArgs e)
    {
        if (d.GetType() == typeof(DataGrid))
        {
            DataGrid myGrid = d as DataGrid;

            ObservableCollection<DataGridColumn> Columns = 
                 (ObservableCollection<DataGridColumn>)e.NewValue;

            if(Columns != null)
            {
                myGrid.Columns.Clear();

                if (Columns != null && Columns.Count > 0)
                {
                    foreach (DataGridColumn dataGridColumn in Columns)
                    {
                        myGrid.Columns.Add(dataGridColumn);
                    }
                }


                Columns.CollectionChanged += delegate(object sender,
                                 NotifyCollectionChangedEventArgs args)
                     {
                       if(args.NewItems != null)
                       {
                         foreach (DataGridColumn column 
                              in args.NewItems.Cast<DataGridColumn>())
                         {
                             myGrid.Columns.Add(column);
                         }
                       }

                       if(args.OldItems != null)
                       {
                         foreach (DataGridColumn column 
                                 in args.OldItems.Cast<DataGridColumn>())
                         {
                           myGrid.Columns.Remove(column);
                         }
                       }
                    };
            }
        }
    }
}

然后像这样将它附加到 DataGrid(其中列是视图模型上的 ObservableCollection 属性)

Then you attach it to you DataGrid like this (Where columns is the an ObservableCollection property on your view model)

<Controls:DataGrid AutoGenerateColumns="False" 
            DataGridExtension.Columns="{Binding Columns}" />

如果您开始添加和删除列,我不确定它的响应情况如何,但从我的基本测试来看,它似乎有效.祝你好运!

I'm not sure how well it is going to respond if you start adding and remove columns, but it seems to work from my basic testing. Good Luck!

这篇关于WPF MvvM DataGrid 动态列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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