8,737
社区成员
发帖
与我相关
我的任务
分享
两个ComboBox
<ComboBox Name="comboBox1" ItemsSource="{Binding Path=List1}" DisplayMemberPath="Name" />
<ComboBox Name="comboBox2" ItemsSource="{Binding Path=List2}" DisplayMemberPath="Model" />
comboBox2怎么设置 IsEnabled="{Binding ElementName=comboBox1, Path=?}"
Path设置成什么 才能达到 : 只有用户在comboBox1中进行了操作选择,comboBox2才启用
也就是若用户没有在 comboBox1中进行任何选择操作前 comboBox2是不启用状态
新增一个转换器类 ,代码和窗体类放同个文件。
public class ComboxEnableConver:IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ //value是comboBox1.SelectedItem
return (value!=null);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
}
增加转换器后,xaml中引入该转换器为资源
<Window.Resources>
<local: ComboxEnableConver x:Key="cbec"/>
<Window.Resources>
comboBox2设置 IsEnabled="{Binding ElementName=comboBox1,Path=SelectedItem,Converter={StaticResource cbec }}"
selecteItem不为空,通过converter来判断