【问题标题】:Not able to bind IsEanbled property of a button to a textBox.Text.Length无法将按钮的 IsEnabled 属性绑定到 textBox.Text.Length
【发布时间】:2015-09-24 04:12:56
【问题描述】:

我正在尝试将按钮的 IsEnabled 属性绑定到 TextBox.Text.Length,但在某处我没有这样做。 想法是仅在 TextBox 是否包含某些文本时启用/禁用按钮。

粘贴下面的示例代码,请帮助我。 问候,

迪拉杰

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BindButtonToTextBox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private bool isTextEntered;

        public event PropertyChangedEventHandler PropertyChanged;
        public bool EnableOKButton
        {
            get { return isTextEntered; }
            set
            {
                if (textBoxOne.Text.Length != 0)
                    isTextEntered = true;
                else
                    isTextEntered = false;

                OnTextEneterd("EnableOKButton");
            }
        }

        protected virtual void OnTextEneterd(string propValue)
        {
            if(PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propValue));
            }
        }
        public MainWindow()
        {
            InitializeComponent();
            //button.IsEnabled = false;
        }
    }
}

【问题讨论】:

  • 使用转换器检查文本是否为空/不为空并在按钮的 IsEnabled 属性上返回一个布尔值(绑定到 TextBox 的文本)
  • @Nikita:可以给我看一些例子或者一些示例代码。

标签: c# .net wpf windows xaml


【解决方案1】:

这是一个伪代码,需要使用准确的语义进行修改(冒着投票的风险:P): 1.转换器:

public class textToEnabledConverter:IValueConverter
{
   ...convert(...)
   {
     if(String.IsnullOrEmpty(value))
    {
      return false;
    }
     return true;
  }
   ...convertBack(...)
  {
    //do not modify
  }
}

xaml(定义ns为转换器类所在的命名空间):

<Windows.Resources>
<ns:textToEnabledConverter x:key=con/>
</Windows.Resources>
<TextBox x:name="txtData"/>
<Button IsEnabled="{Binding ElementName=txtData,Path=Text,Converter={StaticResource con}}"/>

【讨论】:

    【解决方案2】:

    您也可以在视图模型中使用属性。

    private string _inputString;
    public string InputString {
      get { return _inputString; }
      set {
        _inputString = value;
        RaisePropertyChanged("InputString");
        RaisePropertyChanged("IsValidLength");
      }
    }
    public bool IsValidLength {
      get { return string.IsNullOrEmpty(InputString) || InputString.Length <= MAX_LENGTH; }
    }
    

    并进行绑定。

    <Button IsEnabled="{Binding IsValidLength}">
      <TextBlock Text="{Binding InputString, UpdateSourceTrigger=PropertyChanged}" />
    </Button>
    

    (请注意,上面的代码没有经过密集测试。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      相关资源
      最近更新 更多