【问题标题】:XAML using radio buttons to enable and disable textbox with databindingXAML 使用单选按钮启用和禁用带有数据绑定的文本框
【发布时间】:2015-04-26 05:11:07
【问题描述】:

我试图弄清楚如何启用和禁用带有单选按钮和数据绑定的文本框。似乎我应该能够将文本框 IsEnabled 绑定到布尔值并修改该值,但我不能让它工作。我想要几组单选按钮和文本框,所以我想要一种通用的方法来处理这个问题。我尝试使用转换器,但由于我没有使用任何 x:Names,我不确定这些在这种情况下是否有用。我可以让他们启用/禁用单选按钮,但这不是我想要做的。我的代码主要显示了我为第一个文本框尝试的解决方案。

Xaml 代码

    <Grid>
    <StackPanel>
        <RadioButton GroupName="grp1" Content="Enable TextBox"  IsEnabled="{Binding Bttn1, Mode=TwoWay}" IsChecked="{Binding Bttn1}" Checked="RadioButton_Checked" Unchecked="RadioButton_UnChecked" />
        <RadioButton GroupName="grp1" Content="Disable TextBox" IsEnabled="{Binding Bttn2}" />
        <TextBox IsEnabled="{Binding Txtbx1, Mode=TwoWay}" BindingGroup="{Binding grp1}" ></TextBox>

        <RadioButton GroupName="grp2" Content="Enable TextBox"  IsEnabled="{Binding Bttn3, Mode=TwoWay}" IsChecked="{Binding Bttn3}" Checked="RadioButton_Checked" Unchecked="RadioButton_UnChecked" />
        <RadioButton GroupName="grp2" Content="Disable TextBox" IsEnabled="{Binding Bttn4}" />
        <TextBox IsEnabled="{Binding Txtbx2, Mode=TwoWay}" BindingGroup="{Binding grp2}" ></TextBox>
    </StackPanel>
</Grid>

ViewModel 代码

    private bool _bttn1;
    private bool _bttn2;
    private bool _bttn3;
    private bool _bttn4;
    private bool _txtbx1;
    private bool _txtbx2;

    public bool Bttn1
    {
        get
        {
            return(_bttn1);
        }

        set
        {
            _bttn1 = value;
            _txtbx1 = false;
            RaisePropertyChanged(Txtbx1.ToString());
        }

    }

    public bool Bttn2
    {
        get
        {
            return (_bttn1);
        }

        set
        {
            _bttn1 = value;
            _txtbx1 = false;
            RaisePropertyChanged(Txtbx1.ToString());
        }

    }

    public bool Txtbx1
    {
        get
        {
            return (_txtbx1);
        }

        set
        {
            _txtbx1 = false;
        }
    }

往下看

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }


    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        Handle(sender as RadioButton);
    }

    private void RadioButton_UnChecked(object sender, RoutedEventArgs e)
    {
        Handle(sender as RadioButton);
    }


    void Handle(RadioButton radioButton)
    {
        bool flag = radioButton.IsChecked.Value;

        this.Title = "IsChecked = " + flag.ToString();

    }

    public class RadioConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !(bool)parameter;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !(bool)value ? parameter : null;
        }

    }

【问题讨论】:

    标签: c# xaml mvvm


    【解决方案1】:

    如果您想启用和禁用带有单选按钮的文本框,此解决方案有效:

     <Grid>
            <StackPanel>
                <RadioButton x:Name="RB1" GroupName="grp1" Content="Enable TextBox" />
                <RadioButton GroupName="grp1" Content="Disable TextBox"  />
                <TextBox IsEnabled="{Binding IsChecked, ElementName=RB1}" ></TextBox>
    
                <RadioButton x:Name="RB2" GroupName="grp2" Content="Enable TextBox"  />
                <RadioButton GroupName="grp2" Content="Disable TextBox"  />
                <TextBox IsEnabled="{Binding IsChecked, ElementName=RB2}"  ></TextBox>
            </StackPanel>
    

    【讨论】:

    • 感谢您的回答,我希望有一种方法可以在不使用 x:Name 但将 IsEnabled 绑定到变量名并在代码中设置 true 或 false 的情况下禁用文本框,也许这是不可能的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多