动态地将列添加到WPF DataGrid中 - 类似于行的DataTemplates [英] Adding Columns Dynamically into WPF DataGrid - similar to DataTemplates for Rows

查看:191
本文介绍了动态地将列添加到WPF DataGrid中 - 类似于行的DataTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想动态地将列添加到WPF DataGrid中。


我想决定如何定义列,而不是定义列。要动态添加到视图中的许多列。


不想使用以下代码,因为它允许后端代码访问UI。 


  DataGridTextColumn c1 = new DataGridTextColumn();


dataGrid.Columns.Add(c1);



相反,像数据绑定,这将允许我根据我在View Model中给出的计数创建列。


有什么办法吗?


请帮助我,


谢谢,


Sasikala




解决方案

嗨SasiMuthuSella,


如果你想使用MVVM模式,我们可以使用ObservableCollection< dynamic>实现它。这里有一个供你参考的样本。


#XAML

< Window x:Class =" ColeWPFSample.DataGridSample。窗口1" 
xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local =" clr-namespace:ColeWPFSample.DataGridSample"
mc:Ignorable =" d"
Title =" Window1"高度= QUOT; 450"宽度= QUOT; 800">
< Grid>
< DataGrid x:Name =" grid1" ItemsSource =" {Binding Vehicles}">
< / Grid>
< / Window>

#Code Behind

 public partial class Window1:Window 
{
public Window1()
{
InitializeComponent();
this.DataContext = new Window1ViewModel();
}
}

#ViewModel

 public class Window1ViewModel 
{
public ObservableCollection< dynamic>车辆{获得;组;

public Window1ViewModel()
{
using(var db = new ColeContext())
{
var vehs = db.Vehicles.Select( t => new {TitleID = t.Id,TitleMake = t.Make,TitleReg = t.RegNumber,TitleTest =" test"})。ToList();

Vehicles = new ObservableCollection< dynamic>(vehs);
}
}

}


祝你好运,


张龙


Hi,

I want to add columns dynamically into WPF DataGrid.

Instead of defining columns, I want to decide how many columns to add into view dynamically.

I don't want to use the below code, as it allows backend code to access UI. 

 DataGridTextColumn c1 = new DataGridTextColumn();

dataGrid.Columns.Add(c1);

Instead, something like databinding which will allow me to create columns based on count that I am giving in View Model.

Is there any way?

Please help me,

Thank you,

Sasikala


解决方案

Hi SasiMuthuSella,

If you want to use MVVM pattern, we could use ObservableCollection<dynamic> to achieve it. here is a sample for your reference.

#XAML

<Window x:Class="ColeWPFSample.DataGridSample.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ColeWPFSample.DataGridSample"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="grid1" ItemsSource="{Binding Vehicles}"/>
    </Grid>
</Window>

#Code Behind

public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new Window1ViewModel();
        }
    }

#ViewModel

public class Window1ViewModel 
    {
        public ObservableCollection<dynamic> Vehicles { get; set; }

        public Window1ViewModel()
        {
            using (var db = new ColeContext())
            {
                var vehs = db.Vehicles.Select(t=> new { TitleID = t.Id, TitleMake = t.Make, TitleReg = t.RegNumber, TitleTest ="test"}).ToList();

                Vehicles = new ObservableCollection<dynamic>(vehs);
            }
        }

    }

Best regards,

Zhanglong


这篇关于动态地将列添加到WPF DataGrid中 - 类似于行的DataTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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