wpf - 在 Xaml 中启用/禁用对 ComboBox 选择的控件

标签 wpf xaml

如果选择/未选择组合框,如何启用/禁用文本框、标签、文本 block 等控件?例如如果选定的索引大于零,则启用控件,否则禁用。如何将控件的 IsEnabled 属性与组合框选择绑定(bind)?

最佳答案

可以绑定(bind)IsEnabledSelectedIndex组合框的属性并使用 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/

相关文章:

.net - 在WPF世界中有6个月没有使用过的混合物,我应该使用它吗?

c# - 使用 .resx 文件本地化 Prism 4 模块

wpf - 我可以在运行时将 XML/XAML 转换为 WPF 控件吗?

wpf - 水平滚动条未出现在 DataGrid 中

c# - 从开关中的字符串名称返回等效对象

c# - 将数据绑定(bind)/设置源到我的 DataGrid [WPF] 的正确方法是什么

c# - 如何将 TextBlocks 动态添加到 RelativePanel?

c# - ListView中的虚拟条目

c# - 如何覆盖 DataGridRow 模板中的 AlternatingRowBackground?

c# - 不同 DataGrid 行的不同选择颜色