WPF - 将一个组合框的选定项更改为另一个组合框的选定项
WPF - change the selected Item of one combobox to the selected item of another combobox
我的 .xaml 文件中有两个组合框。我会调用第一个组合框 "main combo box "。另一个组合框也包含与第一个主组合框相同的一组值。
当我更改第一个组合框中的选择时,我希望将另一个组合框的选择更改为相同的值。
我已经按照下面的方式完成了。
在我的视图模型中,我有以下内容。
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" />
MyData 是 MyClass 的 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 ;
}
}
我的 .xaml 文件中有两个组合框。我会调用第一个组合框 "main combo box "。另一个组合框也包含与第一个主组合框相同的一组值。
当我更改第一个组合框中的选择时,我希望将另一个组合框的选择更改为相同的值。
我已经按照下面的方式完成了。
在我的视图模型中,我有以下内容。
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" />
MyData 是 MyClass 的 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 ;
}
}