我认为我有两个组合框。
我想要的是当我在ComboBox1上选择值1时,ComboBox2变为启用状态。但是,当我在ComboBox1中选择除1以外的任何其他值时,ComboBox2将保持禁用状态。
答案 0 :(得分:0)
您的XAML:
<ComboBox x:Name="cb1" Margin="10,0" IsReadOnly="True" IsEditable="True" Text="Please select" HorizontalAlignment="Center" >
<ComboBoxItem x:Name="cbi11" Content="Option1" HorizontalAlignment="Left" Width="198" Selected="cbi11_Selected" />
<ComboBoxItem x:Name="cbi12" Content="Option2" HorizontalAlignment="Left" Width="198" Selected="cbi12_Selected"/>
<ComboBoxItem x:Name="cbi13" Content="Option3" HorizontalAlignment="Left" Width="198" Selected="cbi13_Selected"/>
</ComboBox>
<ComboBox x:Name="cb2" Margin="10,0" IsReadOnly="True" IsEditable="True" Text="Please select" HorizontalAlignment="Center" >
<ComboBoxItem x:Name="cbi21" Content="Option1" HorizontalAlignment="Left" Width="198" Selected="cbi21_Selected" />
<ComboBoxItem x:Name="cbi22" Content="Option2" HorizontalAlignment="Left" Width="198" Selected="cbi22_Selected"/>
<ComboBoxItem x:Name="cbi23" Content="Option3" HorizontalAlignment="Left" Width="198" Selected="cbi23_Selected"/>
</ComboBox>
你的守则背后:
private void cbi11_Selected(object sender, RoutedEventArgs e)
{
cb2.IsEnabled = true;
}
private void cbi12_Selected(object sender, RoutedEventArgs e)
{
cb2.IsEnabled = false;
}
private void cbi13_Selected(object sender, RoutedEventArgs e)
{
cb2.IsEnabled = false;
}
答案 1 :(得分:0)
如果您正确地执行此操作,那么您提出错误的问题,因为您的视图模型将为您完成大部分工作
假设你有一个像这样的ViewModel(使用Prism)
public class VM:BindableBase
{
public ICollectionView Combo1Options {get,set}
public ICollectionView Combo2Options {get,set}
private object _Combo1Value;
private object _Combo2Value;
public object Combo1Value
{
get { return _Combo1Value; }
set
{
if(SetProperty(ref _Combo1Value, value))
{
Combo2Options .Refresh();
PropertyChanged(nameof(Combo2Enabled));
}
}
}
public object Combo2Value
{
get { return _Combo2Value; }
set { SetProperty(ref _Combo2Value, value); }
}
public bool Combo2Enabled => Combo1Value != null;//or what ever logic defines if combo2 is required
}
然后你只需绑定到你的视图模型
<ComboBox ItemSource={Binding Combo1Options }, SelectedItem={Binding Combo1Value }/>
<ComboBox ItemSource={Binding Combo2Options }, SelectedItem={Binding Combo2Value } IsEnabled={Binding Combo2Enabled}/>
通过使用CollectionView,您可以过滤组合框项目,因此我会推荐它