简体   繁体   中英

Wpf multiple combobox binding to one property

I am having a data grid, one of its cells is a combo box like:

<DataGrid x:Name="Applications"  RowStyle="{StaticResource CollapsedRow}" AutoGenerateColumns="false" CanUserAddRows="false" ItemsSource="{Binding Applications}">

<DataGrid.Columns>
   <DataGridTemplateColumn>
     <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
         <Button Content='&#709;' FontSize="9" Name="ExpanderButton" Click="OnGroupChange" />
      </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

 <DataGridTextColumn Width="181" Header="Name" Binding="{Binding Name, Mode=OneWay}" />

</DataGrid.Columns>

<DataGrid.RowDetailsTemplate>
 <DataTemplate>
  <ComboBox ItemsSource="{Binding Path=DataContext.Cabins, 
       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" 
       SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"
       SelectedValue="{Binding Path=DataContext.SelectedCabin,
       RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
       mah:TextBoxHelper.Watermark="{Binding Path=DataContext.CabinsWatermark, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
                                                                      Height="2" Width="300" Margin="10 5 10 10" HorizontalAlignment="Left">
 <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Converter={StaticResource GuidConverter}}"/>
    </DataTemplate>
</ComboBox.ItemTemplate> 
</ComboBox>
 </DataTemplate>
</DataGrid.RowDetailsTemplate>

</DataGrid>

And as you see in each row there ia a combo box in detail row (expanded row using button), each combo box is binded to one property:

 private Guid? selectedCabin;
    public override  Guid? SelectedCabin
    {
        get => selectedCabin;
        set
        {
            selectedCabin = value;
            if (value.HasValue)
            {
                Console.WriteLine(value);
            }

            OnPropertyChanged();
        }

Now problem is when i select item in combo box i am getting not single value but couple of them (I suppose there are all values from one combo box I made a selection on), to make sure i double checked with test code behind:

     private void ComboBox_OnSelectCabinChanged(object sender, RoutedEventArgs e)
    {
        var combo = (ComboBox)sender;

        if (combo != null && combo.IsDropDownOpen)
        {
            ((ApplicationsViewModel)DataContext).SelectedCabin = (Guid?)sender;
            combo.IsDropDownOpen = false;
        }
    }

And I am getting here and combo box item list and casting exception. What could be the root cause of this and is there a way to bind multiple combo box values to one property, so is i select one it will override another.

It seems like you are binding the SelectedValue of all row details ComboBoxes to the same source property. And you can't cast the sender argument to a Guid? . Try to cast the SelecteedValue property of the ComboBox :

SelectedCabin = (Guid?)combo.SelectedValue;

If you don't want to handle the SelectionChanged event in the view, you could use an interaction trigger that executes a command that sets the source property. Please refer to this blog post for more information about this.

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