简体   繁体   中英

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

I am currently using an MVVM architecture. I have simplified my code to explain my question. The main idea is that I have a Model (Combo.cs), a class for Combo Services (ComboServices.cs) that will perform operations on the object Combo, a view model (ViewModel.cs) which holds the data for the 2 windows (window 1 and window 2) and 2 views (Window 1 and 2)

Window 1 has a ListBox that is binded to CombosList (ObservableCollection) which is a property in my ViewModel. I then select one object (binded to ObjCombo) from the list and open (click modify button binded to the ModifyComboCommand) a new window (window 2) where I want to view the properties of this object in a DataGrid.

My DataGrid should have the first column as a Combobox while the second column should be a textbox. These two columns should auto-populate with the values of the ObjCombo.LoadCasesInCombo which is a Dictionary<string,double>. How would I auto-populate the dictionary values (keys in the first column and values in the second column) while also having my Combobox in my first column only display values from the CombosList in the drop down. I also cant have duplicate values in my First column.

See rough picture here of intended behavior:



Model (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;
            }
        }
    }

My ViewModel (ViewModel.cs) contains the properties I want to databind to my datagrid in my view.

    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.
        }

    }

Option 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>

Option 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>`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM