如果选择/未选择组合框,如何启用/禁用文本框、标签、文本 block 等控件?例如如果选定的索引大于零,则启用控件,否则禁用。如何将控件的 IsEnabled 属性与组合框选择绑定(bind)?
最佳答案
可以绑定(bind)IsEnabled
到SelectedIndex
组合框的属性并使用 IValueConverter
将其转换为 bool 值。例如,在您的 XAML 中(显示启用 Button
):
<ComboBox x:Name="cmbBox" ItemsSource="{Binding Source={StaticResource DataList}}"/>
<Button Grid.Column="1" IsEnabled="{Binding ElementName=cmbBox, Path=SelectedIndex, Converter={StaticResource IndexToBoolConverter}}"/>
然后你还需要一个转换器,例如:
public class IndexToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if ((int)value > 0)
{
return true;
}
else
{
return false;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
您还需要将 Converter 声明为资源,例如在您的 Window 中。
<local:IndexToBoolConverter x:Key="IndexToBoolConverter"/>
关于wpf - 在 Xaml 中启用/禁用对 ComboBox 选择的控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37813341/