3

我有一个 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 主题的,我认为它缺少一些我可以玩的资源。

任何帮助将非常感激!

4

1 回答 1

2

对于问题的第一部分,即编辑部分,您必须将列的“绑定”属性绑定到用户编辑单元格时要修改的相关 ViewModel 属性。由于您是从 .cs 文件创建列,因此您可以这样做:

case SuperTapiModel.ColumnType.DataGridTextColumn:
{
    DataGridTextColumn dgcText = new DataGridTextColumn();
    dgcText.Binding = new Binding("The name of the property");
    newColumn = dgcText;
}
break;

请注意,我以前从未真正这样做过,所以语法可能有点不同,但你明白了。

对于第二部分,设置 EditingElementStyle,您将执行与 Binding 相同的操作。在 Xaml 文件中创建您的样式,然后使用 FindResource 方法在 .cs 文件中设置 EditingElementStyle 的值。它看起来像这样:

case SuperTapiModel.ColumnType.DataGridTextColumn:
{
    DataGridTextColumn dgcText = new DataGridTextColumn();
    dgcText.EditingElementStyle = FindResource("YourResourceKey") as Style;
    newColumn = dgcText;
}
break;

我希望这会有所帮助!

于 2019-11-18T11:24:07.263 回答