[英]How can I make a Button stay disable conected to Textblock in XAML?
I have a TextBox
that i want it to control a Button
. 我有一个想要让它控制
Button
的TextBox
。 that as long as nothing writen in the TextBox
the Button
will be dissabled. 只要在
TextBox
没有写入任何内容,该Button
就会被禁用。
You can accomplish this all in XAML
using a Style
DataTrigger
. 您可以使用
Style
DataTrigger
在XAML
完成所有这些DataTrigger
。
<TextBlock x:Name="myTextBlock" />
<Button>
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text.Length, ElementName=myTextBlock}" Value="0">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Troggers>
</Style>
</Button.Style>
</Button>
you have to use data binding and a converter 您必须使用数据绑定和转换器
<TextBlock x:Name="textBlock" />
<Button IsEnabled="{Binding ElementName=textBlock, Path=Text, Converter={StaticResource TextToBoolConverter}}" />
and the converter looks like this: 转换器看起来像这样:
public class TextToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var text = value as string;
if (text.Length > 0)
{
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value;
}
}
and this goes in your page: 这在您的页面中:
<Page.Resources>
<local:TextToBoolConverter x:Name="TextToBoolConverter" />
</Page.Resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.