启用/禁用 Xaml 中组合框选择的控件 [英] Enable/Disable controls on ComboBox selection in Xaml

查看:29
本文介绍了启用/禁用 Xaml 中组合框选择的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to enable /disable controls like textbox,label,textblock if combobox is selected/not-selected? e.g. If selected index is greater than zero, enable controls else disable.How to bind IsEnabled properties of the control with combobox selection?

推荐答案

您可以将 IsEnabled 绑定到 ComboBox 的 SelectedIndex 属性并使用 IValueConverter 将其转换为布尔值.例如,在您的 XAML 中(显示启用 Button):

You can bind IsEnabled to the SelectedIndex property of the ComboBox and use a IValueConverter to convert it to Boolean. For instance, in your XAML (showing enabling a Button):

<ComboBox x:Name="cmbBox" ItemsSource="{Binding Source={StaticResource DataList}}"/>
<Button Grid.Column="1" IsEnabled="{Binding ElementName=cmbBox, Path=SelectedIndex, Converter={StaticResource IndexToBoolConverter}}"/>

那么你还需要一个转换器,比如:

Then you need a converter as well, such as:

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 声明为资源,例如在您的窗口中.

You'll also have declare the Converter as a resource, such as in your Window.

<local:IndexToBoolConverter x:Key="IndexToBoolConverter"/>

这篇关于启用/禁用 Xaml 中组合框选择的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆