Datagrid (WPF) 中的绑定字典值 Combobox 和 TextBox 列

Binding Dictionary values Combobox and TextBox columns in Datagrid (WPF)

我目前正在使用 MVVM 架构。我简化了我的代码来解释我的问题。主要思想是我有一个模型(Combo.cs),一个用于组合服务(ComboServices.cs)的class,它将对对象组合执行操作,一个视图模型(ViewModel.cs) 其中包含 2 个 windows(window 1 和 window 2)和 2 个视图(Window 1 和 2)

的数据

Window 1 有一个绑定到 CombosList (ObservableCollection) 的 ListBox,它在我的 ViewModel 中是一个 属性。然后我从列表中 select 一个对象(绑定到 ObjCombo)并打开(单击绑定到 ModifyComboCommand 的修改按钮)一个新的 window (window 2) 我想在其中查看属性DataGrid 中的此对象。

我的 DataGrid 应该将第一列作为组合框,而第二列应该是文本框。这两列应自动填充 ObjCombo.LoadCasesInCombo 的值,它是一个 Dictionary。我将如何自动填充字典值(第一列中的键和第二列中的值),同时第一列中的 Combobox 仅显示下拉列表中 CombosList 的值。我的第一列中也不能有重复值。

在此处查看预期行为的粗略图片:



型号(Combo.cs)

public class Combo : INotifyPropertyChanged
    {
        private string comboname;
        private Dictionary<string, double> loadcasesincombo;

        public string ComboName
        {
            get
            {
                return comboname;
            }
            set
            {
                comboname = value;
            }
        }
       
        public Dictionary<string, double> LoadCasesInCombo
        {
            get
            {
                return loadcasesincombo;
            }
            set
            {
                loadcasesincombo = value;
            }
        }
    }

我的 ViewModel (ViewModel.cs) 包含我想在我的视图中数据绑定到我的数据网格的属性。

    public class ViewModel : INotifyPropertyChanged
    {
        private Combo objcombo;

        public Combo ObjCombo
        {
            get { return objcombo; }
            set { objcombo = value; }
        }
  
        private ComboServices ObjComboServices;

        private ObservableCollection<Combo> combosList;
        public ObservableCollection<Combo> CombosList
        {
            get { return combosList; }
            set { combosList = value; }
        }

        public MainWindowViewModel()
        {
            ObjComboServices = new ComboServices();
            modifycomboCommand = new CombosCommands(Modify);
        }

        #region Modify Combo region
        private CombosCommands modifycomboCommand;

        public CombosCommands ModifyComboCommand
        {
            get { return modifycomboCommand; }
        }

        public void Modify()
        {
            // Open second view with DataGrid

            Dictionary<string, double> temp_loadcasesincombo = ObjCombo.LoadCasesInCombo;
            // this is the dictionary I want to autofill my datagrid 2 columns.
        }

    }

选项 1:

<DataGridComboBoxColumn 
                SelectedValueBinding="{Binding Name}" 
                DisplayMemberPath="Name"  HeaderStyle="{StaticResource CenterGridHeaderStyle}"
                SelectedValuePath="Name" Header="Load Name" Width="300">

                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CombosList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="{x:Type ComboBox}">
                        <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CombosList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>

选项 2:`

                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox IsEditable="True" DisplayMemberPath="Name"  SelectedValuePath="Name" IsTextSearchEnabled="True"
                        Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                        ItemsSource="{Binding Path=DataContext.CombosAndLoadCasesList, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>

                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>

            </DataGridTemplateColumn>`