I'm trying to set the width property of textbox when text is "ABC",however the trigger doesn't work. the width remains '40'.
<TextBox Height="23" HorizontalAlignment="Left" Margin="295,211,0,0" Name="textBox1" VerticalAlignment="Top" Width="40" Text="{Binding Text}" >
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Text" Value="ABC" >
<Setter Property="Width" Value="120"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
You need to remove the Width property in the TextBox definition because this will take precedence over the Trigger. Set the Width in a Style setter as below:
<TextBox Height="23" HorizontalAlignment="Left" Margin="295,211,0,0" Name="textBox1" VerticalAlignment="Top" Text="{Binding Text}" >
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Width" Value="40"/>
<Style.Triggers>
<Trigger Property="Text" Value="ABC" >
<Setter Property="Width " Value="120"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
UPDATE -
Local value has higher precedence compared to triggers. Refer to this - Dependency Property Value Precedence.
Related
In my wpf usercontrol, I have a toggle button like this,
<ToggleButton x:Name="btnExpand" Grid.Row="0" Width="25" Height="25" HorizontalAlignment="Right" Margin="5" >
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsCheckedState}" Value="true">
<Setter Property="Source" Value = "pack://application:,,,/MyAssembly;component/SalesModule/Images/expand.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsCheckedState}" Value="false">
<Setter Property="Source" Value = "pack://application:,,,/MyAssembly;component/SalesModule/Images/collapse.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
But the images are not showing !!! Both Image's BuildAction is already set to Resource in their properties. Then i cleaned the solution and then rebuilded. Still no use.
Could you please help me to display the image inside the toggle button ??
Bind to the IsChecked property of the parent ToggleButton, and use only one DataTrigger and a regular Style Setter:
<ToggleButton x:Name="btnExpand" Grid.Row="0" Width="25" Height="25" HorizontalAlignment="Right" Margin="5" >
<Image Width="20" Height="20">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="pack://application:,,,/MyAssembly;component/SalesModule/Images/collapse.png"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName = btnExpand}" Value="true">
<Setter Property="Source" Value="pack://application:,,,/MyAssembly;component/SalesModule/Images/expand.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</ToggleButton>
I'm trying to write a xaml piece that will set my TextBox's text whenever it gets / loses focus. Currently it looks like this :
<TextBox Name="TextBoxLogin" HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="160">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseCaptured" Value="True">
<Setter Property="Text" Value="test"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
But for some reason whenever I click it, the text doesn't change. I'd like to know why and what are my options to make it work. I also tried other properties like IsStylusCaptured and IsFocused, they didn't work either.
The issue is simple, you are setting the Text property in the first line :( Either set it in the STyle or don't set it :
<TextBox Name="TextBoxLogin" HorizontalAlignment="Left" Height="25" TextWrapping="Wrap" VerticalAlignment="Top" Width="160">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Text" Value = "TextHEre"/>
<Style.Triggers>
<Trigger Property="IsMouseCaptured" Value="True">
<Setter Property="Text" Value="test"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
I have the following xaml:
<DockPanel>
<DockPanel>
<CheckBox IsChecked="{Binding Path=Test}" />
<CheckBox IsChecked="{Binding Path=Test}" />
</DockPanel>
<DockPanel DockPanel.Dock="Left" Width="10" Background="Blue">
<DockPanel.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Test}" Value="True">
<Setter Property="DockPanel.Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
</DockPanel>
</DockPanel>
Now - the 2 checkboxes link properly - checking one will check the other - but the datatrigger is not firing at all.
What am I doing wrong?
The issue here is Property Value Precedence.
You are currently setting the Background to blue directly on the DockPanel. This explicit property will override any value set by the trigger.
Instead, you must set the original "Background" as a setter in the style.
<DockPanel DockPanel.Dock="Left" Width="10">
<DockPanel.Style>
<Style>
<Setter Property="DockPanel.Background" Value="Blue" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Test}" Value="True">
<Setter Property="DockPanel.Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DockPanel.Style>
</DockPanel></DockPanel>
On my Grid I Have TextBlock and Button.
If Button is not visible I want my TextBlock.HorizontalAlignment to be set to Center.
If Button is visible I want my TextBlock.HorizontalAlignment to be set to Right. Here is my code:
<TextBlock Grid.Row="0" VerticalAlignment="Center" Name="myTextBlock" Text="{Binding TileTextId}" TextWrapping="Wrap" TextAlignment="Center" >
<TextBlock.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton, Path=IsVisible}" Value="True">
<Setter Property="HorizontalAlignment" Value="Right" />
</DataTrigger>
</TextBlock.Triggers>
</TextBlock>
I get the error:
'HorizontalAlignment' member is not valid because it does not have a qualifying type name.
So I tried to add TextBlock.HorizontalAlignment, like this:
<TextBlock Grid.Row="0" VerticalAlignment="Center" Name="myTextBlock" Text="{Binding TileTextId}" TextWrapping="Wrap" TextAlignment="Center" >
<TextBlock.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton, Path=IsVisible}" Value="True">
<Setter Property="TextBlock.HorizontalAlignment" Value="Right" />
</DataTrigger>
</TextBlock.Triggers>
</TextBlock>
I get the error:
XamlParseException
How should I do that?
Don't try to use TextBlock.Triggers, instead go for a Style with Style.Triggers.
<StackPanel>
<TextBlock Text="TextBlock Content" Margin="5">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton,Path=IsVisible}" Value="True">
<Setter Property="HorizontalAlignment" Value="Right"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<Button x:Name="myButton" Content="Click Me!" Margin="5"/>
</StackPanel>
Take note of the documentation, as it mentions, why style triggers are needed here.
Note that the collection of triggers established on an element only
supports EventTrigger, not property triggers (Trigger). If you require
property triggers, you must place these within a style or template and
then assign that style or template to the element either directly
through the Style property, or indirectly through an implicit style
reference.
Try to do this with Style
<TextBlock Grid.Row="0" VerticalAlignment="Center" Name="myTextBlock" Text="{Binding TileTextId}" TextWrapping="Wrap" TextAlignment="Center" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myButton, Path=IsVisible}" Value="True">
<Setter Property="HorizontalAlignment" Value="Right" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
I seem to be having a hard time today. All I want to do is make a TextBox hidden of visible based on a bool value databound to the Window its hosted in.
What I have just won't compile and I don't understand why. Please help.
<TextBlock Grid.Column="2" Text="This order will be sent to accounting for approval"
Foreground="Red" VerticalAlignment="Center" FontWeight="Bold" Padding="5">
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AllowedToSubmit}" Value="True">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
You need to set the Style.TargetType in order for it to recognize the Visibility property:
<TextBlock Grid.Column="2" VerticalAlignment="Center" FontWeight="Bold" Foreground="Red" Padding="5" Text="This order will be sent to accounting for approval">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AllowedToSubmit}" Value="True">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
Your binding path to AllowedToSubmit probably needs to have ElementName set to the Window's name, as well.
Another option is to bind TextBlock.Visibility directly to the property:
<Window>
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BoolToVisibility" />
</Window.Resources>
<TextBlock Visibility="{Binding Path=AllowedToSubmit, Converter={StaticResource BoolToVisibility}}" />
</Window>
If you want it to work like in your sample, where true hides the TextBlock, then you can write your own converter to convert opposite of the built-in BooleanToVisibilityConverter.