Reputation: 27
I have a TextBox
that i want it to control a Button
.
that as long as nothing writen in the TextBox
the Button
will be dissabled.
Upvotes: 1
Views: 35
Reputation: 17402
You can accomplish this all in XAML
using a Style
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>
Upvotes: 2
Reputation: 929
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>
Upvotes: 0