【发布时间】: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:可以给我看一些例子或者一些示例代码。