question

zleug-6581 avatar image
0 Votes"
zleug-6581 asked ·

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

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-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

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
 }
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.