棱镜/ MVVM:绑定列到DataGrid中

编程入门 行业动态 更新时间:2024-10-28 21:30:16
本文介绍了棱镜/ MVVM:绑定列到DataGrid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是标准的.NET的DataGrid是这样的:

I'm using a standard .NET DataGrid like this:

<DataGrid ItemsSource="{Binding Datensaetze}" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="my col 1" Binding="{Binding MyCol1}"/> <DataGridTextColumn Header="my col 2" Binding="{Binding MyCol2}"/> <DataGridTextColumn Header="my col 3" Binding="{Binding MyCol3}"/> </DataGrid.Columns> </DataGrid>

这是很好的工作。现在,我想在视图模型来定义列,而不是在XAML中设置固定列我想生成它们的飞行。

This is working nicely. Now I want to define the columns in the ViewModel, and instead of setting fixed columns in xaml I want to generate them on the fly.

我的问题:如果我尝试绑定列到任何东西我得到一个错误,说DataGrid.Columns是一个只读属性,不能被绑定

My problem: if I try to bind the Columns to anything I get an error, saying "DataGrid.Columns is a readonly property and can't be bound".

有没有到DataGrid列动态绑定的东西在code后面的路?

Is there a way to dynamically bind the DataGrid columns to something in code behind?

推荐答案

是,列属性是只读,所以我们不能直接绑定到它。如果你想将列绑定,那么你可以尝试使用您绑定到,这反过来又更新了列附加属性。

Yes, the Columns Property is ReadOnly so we can't bind to it directly. If you want to Bind Columns then you can try to use an attached property which you bind to, which in turn updates the Columns.

更新结果在CollectionChanged事件中使用的变化三角洲。

Update Using delta of changes in the CollectionChanged event.

public class DataGridColumnsBehavior { public static readonly DependencyProperty BindableColumnsProperty = DependencyProperty.RegisterAttached("BindableColumns", typeof(ObservableCollection<DataGridColumn>), typeof(DataGridColumnsBehavior), new UIPropertyMetadata(null, BindableColumnsPropertyChanged)); private static void BindableColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e) { DataGrid dataGrid = source as DataGrid; ObservableCollection<DataGridColumn> columns = e.NewValue as ObservableCollection<DataGridColumn>; dataGrid.Columns.Clear(); if (columns == null) { return; } foreach (DataGridColumn column in columns) { dataGrid.Columns.Add(column); } columns.CollectionChanged += (sender, e2) => { NotifyCollectionChangedEventArgs ne = e2 as NotifyCollectionChangedEventArgs; if (ne.Action == NotifyCollectionChangedAction.Reset) { dataGrid.Columns.Clear(); foreach (DataGridColumn column in ne.NewItems) { dataGrid.Columns.Add(column); } } else if (ne.Action == NotifyCollectionChangedAction.Add) { foreach (DataGridColumn column in ne.NewItems) { dataGrid.Columns.Add(column); } } else if (ne.Action == NotifyCollectionChangedAction.Move) { dataGrid.Columns.Move(ne.OldStartingIndex, ne.NewStartingIndex); } else if (ne.Action == NotifyCollectionChangedAction.Remove) { foreach (DataGridColumn column in ne.OldItems) { dataGrid.Columns.Remove(column); } } else if (ne.Action == NotifyCollectionChangedAction.Replace) { dataGrid.Columns[ne.NewStartingIndex] = ne.NewItems[0] as DataGridColumn; } }; } public static void SetBindableColumns(DependencyObject element, ObservableCollection<DataGridColumn> value) { element.SetValue(BindableColumnsProperty, value); } public static ObservableCollection<DataGridColumn> GetBindableColumns(DependencyObject element) { return (ObservableCollection<DataGridColumn>)element.GetValue(BindableColumnsProperty); } }

然后你可以BindableColumns属性绑定到你的ColumnsCollection

Then you can bind the BindableColumns property to your ColumnsCollection

<DataGrid AutoGenerateColumns="False" local:DataGridColumnsBehavior.BindableColumns="{Binding ColumnCollection}"> <!-- ... --> </DataGrid>

更多推荐

棱镜/ MVVM:绑定列到DataGrid中

本文发布于:2023-11-06 14:56:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564006.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:棱镜   绑定   MVVM   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!