كيفية تمكين الزر عندما يكتب المستخدم في مربع النص
سؤال
ما هو أبسط وسيلة في برنامج الأغذية العالمي لتمكين Button
عندما يكتب المستخدم شيء في TextBox
؟
المحلول
استخدم القيادة بسيط
<TextBox Text={Binding Path=TitleText}/>
<Button Command="{Binding Path=ClearTextCommand}" Content="Clear Text"/>
وهنا هو نموذج التعليمات البرمجية في عرض نموذج
public class MyViewModel : INotifyPropertyChanged
{
public ICommand ClearTextCommand { get; private set; }
private string _titleText;
public string TitleText
{
get { return _titleText; }
set
{
if (value == _titleText)
return;
_titleText = value;
this.OnPropertyChanged("TitleText");
}
}
public MyViewModel()
{
ClearTextCommand = new SimpleCommand
{
ExecuteDelegate = x => TitleText="",
CanExecuteDelegate = x => TitleText.Length > 0
};
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
لمزيد من المعلومات راجع مارلون Grechs <لأ href = "http://marlongrech.wordpress.com/2008/11/26/avoiding-commandbinding-in-the-xaml-code-behind-files/" يختلط = " noreferrer نوفولو "> SimpleCommand
وكما تحقق من MVVM قالب المشروع / أدوات من <لأ href = "http://blogs.msdn.com/llobo/archive/2009/05/01/download-mv-vm-project-template-toolkit. ASPX "يختلط =" نوفولو noreferrer "> http://blogs.msdn.com/llobo/archive/2009/05/01/download-mv-vm-project-template-toolkit.aspx . ويستخدم DelegateCommand للقيادة وينبغي أن يكون قالب انطلاق عظيم لأي مشروع.
نصائح أخرى
لماذا جعل الجميع الامور معقدة جدا!
<TextBox x:Name="TB"/>
<Button IsEnabled="{Binding ElementName=TB,Path=Text.Length}">Test</Button>
وأي شيء آخر حاجة ......
إذا كنت لا تستخدم أوامر، بديل آخر يستخدم محول.
وعلى سبيل المثال، وذلك باستخدام الباحث عام لمنطقي تحويل:
[ValueConversion(typeof(int), typeof(bool))]
public class IntToBoolConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
return (System.Convert.ToInt32(value) > 0);
}
catch (InvalidCastException)
{
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToBoolean(value) ? 1 : 0;
}
#endregion
}
وبعد ذلك على الأزرار الملكية ISENABLED:
<Button IsEnabled={Binding ElementName=TextBoxName, Path=Text.Length, Converter={StaticResource IntToBoolConverter}}/>
وHTH،
ودينيس
استخدم مشغل!
<TextBox x:Name="txt_Titel />
<Button Content="Transfer" d:IsLocked="True">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=txt_Titel, Path=Text}" Value="">
<Setter Property="Button.IsEnabled" Value="false"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
والمفتاح في هذا على الربط نفسها ..
إضافة UpdateSourceTrigger = PropertyChanged
وهذا هو الحل أبسط
وإضافة رد إلى مربع نص أن يطلق على كل السكتة الدماغية. اختبار الفراغ في هذا الاستدعاء وتمكين / تعطيل زر.