繁体   English   中英

WPF-将一个组合框的选定项更改为另一组合框的选定项

[英]WPF - change the selected Item of one combobox to the selected item of another combobox

我的.xaml文件中有两个组合框。 我将第一个组合框称为“主组合框”。 另一个组合框还包含与第一个主组合框相同的一组值。

当我在第一个组合框中更改选择时,我希望另一个组合框的选择更改为相同的值。

我已经完成了以下操作。

在我的视图模型中,我有以下内容。

  private <MyClass> _firstComboBoxSelection;
  public <MyClass>  FirstComboboxSelection
  {
         set { _firstComboBoxSelection=value; }
         get { return _firstComboBoxSelection ; }

  }

   private <MyClass> _secondComboBoxSelection;
   public <MyClass>  SecondComboboxSelection
   {
         set { _secondComboBoxSelection=value; }
         get { return _secondComboBoxSelection ; }

    }

组合框如下所示。

       <ComboBox Name="cmbFirst"
                 SelectionChanged="cmbFirst_SelectionChanged"
                 SelectedItem="{Binding FirstComboboxSelection,Mode=TwoWay}"
                 ItemSource="{Binding MyData}"
                 DisplayMemberPath="Name" />

       <ComboBox SelectedItem="{Binding SecondComboboxSelection,Mode=TwoWay}"
                 ItemSource="{Binding MyData}"
                 DisplayMemberPath="Name" />

MyDataMyClass的ObservableCollection。 MyClass包含属性Name 在我的.xaml.cs文件中,我有以下内容。

  private void cmbFirst_SelectionChanged(...)
  {

        _secondComboBoxSelection=_firstComboBoxSelection;
  }

但是它并没有改变我想要的第二个组合框。 有人可以帮我弄清楚我哪里出了问题吗?

在您第二个组合框更改中

  <ComboBox SelectedItem="{Binding SecondComboboxSelection}"

 <ComboBox SelectedItem="{Binding FirstComboboxSelection}"

您也可以尝试像这样使用SelectedValuePath

  <ComboBox Name="cmbFirst"
                 SelectionChanged="cmbFirst_SelectionChanged"
                 SelectedItem="{Binding FirstComboboxSelection,Mode=TwoWay}"
                 ItemSource="{Binding MyData}"
                 SelectedValuePath="Name" 
                 DisplayMemberPath="Name" />

在代码中,您可以执行以下操作-

  private <MyClass> _firstComboBoxSelection;
  public <MyClass>  FirstComboboxSelection
  {
         set { _firstComboBoxSelection=value;
                 OnPropertyChanged(_firstComboBoxSelection ); }
         get { return _firstComboBoxSelection ;
                }

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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