Display DataGrid record value from one form in ComboBox in another form

zleug 61 Reputation points
2020-05-27T12:26:10.023+00:00

Hi All. I have one form with DataGrid where each record row has button that open another form to edit the according record. Both form link to EF table. When I click the button in DataGrid the TextBoxes are display record values from DataGrid but ComboBoxes does not. How to display value in ComboBox? I will appreciate for sample and explanation. Thanks.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-05-28T03:08:39.66+00:00

    Welcome to our Microsoft Q&A platform!

    I will provide some methods to bind the data for ComboBox in DataGrid for you to try.

    Method 1:

    <Page.Resources>
            <CollectionViewSource x:Key="ItemsCVS" Source="{Binding ColumnItems}" />
    </Page.Resources>
    
    …
    
     <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource ItemsCVS}}"  Header="Gender" SelectedValueBinding="{Binding ColumnID}"  DisplayMemberPath="Name"  SelectedValuePath="ID"
                                             >
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ColumnItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ColumnItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
    

    Method 2:

    <DataGridComboBoxColumn  SelectedValueBinding="{Binding ColumnID}"  DisplayMemberPath="Name"  SelectedValuePath="ID">
                        <DataGridComboBoxColumn.ElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ColumnItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.ElementStyle>
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="ItemsSource" Value="{Binding Path=DataContext.ColumnItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>
    

    Method 3:

    <DataGridTemplateColumn Header="MyComboBox">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox
                    ItemsSource="{Binding DataContext.Column, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                    SelectedValuePath="MColumn.Id"
                    SelectedValue="{Binding ColumnId, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                    >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Rectangle Fill="{Binding ColumnBrush}" Height="30" Width="30"/>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    Thanks.

    0 comments No comments

  2. Peter Fleischer (former MVP) 19,316 Reputation points
    2020-05-31T07:54:49.633+00:00

    Hi, you can include in ViewModel a view with list for your Combobox. When you create instance of second form you can add resource with instance of ViewModel (not new instance!). After loading second form (Loding event) you can bind Combobox.

    When click any Row in first form execute "LoadForm" to show second form for selected row ("Table1" item)

    private void LoadForm(Table1 d)
    {
      SecondForm wnd = new SecondForm();
      wnd.DataContext = d;
      wnd.Resources.Add("vm0", this);
      wnd.Show();
    }
    

    In second form:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      Binding b = new Binding("LookUp"); // field in "Table1"
      b.Source = this.Resources["vm0"];
      cb.SetBinding(ComboBox.ItemsSourceProperty, b); // cb - Combobox in second form
    }
    
    0 comments No comments