我有一个 DataGrid 前端代码,如下所示:
<DataGrid ItemsSource="{Binding DTDisplayData}" x:Name="dgReport"
attached:DataGridColumnsAttched.BindableColumns="{Binding ColumnCollection}"
AutoGenerateColumns="False" CanUserDeleteRows="False" IsReadOnly="False"
CanUserSortColumns="True" CanUserAddRows="False" Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="3" BorderBrush="#89000000" BorderThickness="1"
>
<DataGrid.Style>
<Style TargetType="DataGrid" BasedOn="{StaticResource MaterialDesignDataGrid}">
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedBox}" Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
</DataGrid>
在后端.CS代码中附加了一个类:
public class DataGridColumnsAttched
{
public static readonly DependencyProperty BindableColumnsProperty =
DependencyProperty.RegisterAttached("BindableColumns",
typeof(ObservableCollection<DataGridColumnMetaData>),
typeof(DataGridColumnsAttched),
new UIPropertyMetadata(null, BindableColumnsPropertyChanged));
private static void BindableColumnsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
DataGrid dataGrid = source as DataGrid;
ObservableCollection<DataGridColumnMetaData> columns = e.NewValue as ObservableCollection<DataGridColumnMetaData>;
dataGrid.Columns.Clear();
if (columns == null)
{
return;
}
foreach (DataGridColumnMetaData column in columns)
{
dataGrid.Columns.Add(CreateColumnFromMetaData(column));
}
columns.CollectionChanged += (sender, e2) =>
{
NotifyCollectionChangedEventArgs ne = e2 as NotifyCollectionChangedEventArgs;
if (ne.Action == NotifyCollectionChangedAction.Reset)
{
dataGrid.Columns.Clear();
if(ne.NewItems != null)
{
foreach (DataGridColumnMetaData column in ne.NewItems)
{
dataGrid.Columns.Add(CreateColumnFromMetaData(column));
}
}
}
else if (ne.Action == NotifyCollectionChangedAction.Add)
{
foreach (DataGridColumnMetaData column in ne.NewItems)
{
dataGrid.Columns.Add(CreateColumnFromMetaData(column));
}
}
else if (ne.Action == NotifyCollectionChangedAction.Move)
{
dataGrid.Columns.Move(ne.OldStartingIndex, ne.NewStartingIndex);
}
else if (ne.Action == NotifyCollectionChangedAction.Remove)
{
foreach (DataGridColumnMetaData column in ne.OldItems)
{
dataGrid.Columns.Remove(CreateColumnFromMetaData(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);
}
public static DataGridColumn CreateColumnFromMetaData(DataGridColumnMetaData columnMetaData)
{
DataGridColumn newColumn;
switch (columnMetaData.Type)
{
case SuperTapiModel.ColumnType.DataGridTextColumn:
{
DataGridTextColumn dgcText = new DataGridTextColumn();
//set parameters....
newColumn = dgcText;
}
break;
case SuperTapiModel.ColumnType.DataGridTemplateColumn:
{
DataGridTemplateColumn dgcTemplate = new DataGridTemplateColumn();
newColumn = dgcTemplate;
}
break;
case SuperTapiModel.ColumnType.DataGridHyperlinkColumn:
{
DataGridHyperlinkColumn dgcHyperlink = new DataGridHyperlinkColumn();
newColumn = dgcHyperlink;
}
break;
case SuperTapiModel.ColumnType.DataGridComboBoxColumn:
{
DataGridComboBoxColumn dgcComboBox = new DataGridComboBoxColumn();
newColumn = dgcComboBox;
}
break;
case SuperTapiModel.ColumnType.DataGridCheckBoxColumn:
{
DataGridCheckBoxColumn dgcCheckBox = new DataGridCheckBoxColumn();
newColumn = dgcCheckBox;
}
break;
default:
{
DataGridTextColumn dgcText = new DataGridTextColumn();
//set parameters....
newColumn = dgcText;
}
break;
}
return newColumn;
}
}
我想在数据网格中编辑数据,我假设我必须在编辑PropertyChanged
单元格时捕获编辑事件。
然后,我想EditingElementStyle
在后端 .CS 代码中设置参数。
如何使用 WPF 和 MVVM 实现它?
值得一提的是,我的代码源是基于 MaterialDesign 主题的,我认为它缺少一些我可以玩的资源。
任何帮助将非常感激!