Reputation: 4875
I need to Write DataTrigger to update the value of a Tag Property in a WPF TextBox.
If the TextBox Text.Count >0
then update the Tag
property to True
otherwise False
.
XAML Source Code:
<TextBox Text="WPF" Tag="True">
<TextBox.Triggers>
<DataTrigger Property="Text" Value="0">
<Setter Property="Tag" Value="False" />
</DataTrigger>
</TextBox.Triggers>
</TextBox>
Upvotes: 1
Views: 4265
Reputation: 4706
I've used StevenRands answer and adapted my own:
In this example Tag will be false if Text is null or empty.
<StackPanel>
<TextBox Text="WPF" Name="tb">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Tag" Value="True"/>
<Style.Triggers>
<DataTrigger Value="" Binding="{Binding Text, RelativeSource={RelativeSource Self}}">
<Setter Property="Tag" Value="False" />
</DataTrigger>
<DataTrigger Value="{x:Null}" Binding="{Binding Text, RelativeSource={RelativeSource Self}}">
<Setter Property="Tag" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<!-- YOU CAN CHECK THE TAG'S VALUE HERE-->
<TextBlock Text="{Binding ElementName=tb, Path=Tag}"/>
</StackPanel>
And here's StevenRands better answer with some changes to make it work:
<TextBox Text="WPF">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Tag" Value="True"/>
<Style.Triggers>
<DataTrigger Value="0" Binding="{Binding Text.Length, RelativeSource={RelativeSource Self}}">
<Setter Property="Tag" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Upvotes: 2
Reputation: 5421
Your code won't work because you can't put data triggers into a control's Triggers
collection. What you actually need is a trigger in the control's Style
.
Try this instead:
<TextBox Text="WPF" Tag="True">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Value="0"
Binding="{Binding Text.Length, RelativeSource={RelativeSource Self}}">
<Setter Property="Tag" Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Note that this is not foolproof: if the text box contains only whitespace for example, then it may appear to be empty but the length of the text will be greater than zero.
As the answers from user2946329 and ATM show, there are various ways of doing this in a <Style>
trigger.
Upvotes: 4
Reputation: 597
You will have to do something similar to this:
Here I described a style and attached it to a control.
<Style TargetType="telerik:BarIndicator" x:Key="Percent">
<Style.Resources>
<vc:LoadPercentValueConverter x:Key="LPValueConverter"/>
</Style.Resources>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=myEngine.PercentLoaded, Converter={StaticResource LPValueConverter}}" Value="1" >
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=myEngine.PercentLoaded, Converter={StaticResource LPValueConverter}}" Value="0" >
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
Here is the control:
<telerik:BarIndicator Style="{StaticResource Percent}" Visibility="{Binding WhileLoading}" Value="{Binding Path=myEngine.PercentLoaded}" StartWidth="0.13"/>
And you have to use a value converter. Here is mine.
class LoadPercentValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
long percent = (long)System.Convert.ChangeType(value, typeof(long));
if (percent > 80)
{
return 1;
}
else
{
return 0;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Upvotes: 0
Reputation: 39976
DataTrigger
has not the property. You shoud use Property trigger
for this purpose like this:
<TextBox Text="WPF" Tag="True">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="0">
<Setter Property="Tag" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Or if you want to change the Tag
based on text Length you should try this:
<Trigger Property="Text" Value="">
Upvotes: 2