I have a couple text boxes and collection of radio buttons in WPF. I want to use the trigger to set the IsChecked property of oly one radio button which has a name if any text boxes get focus. I check a few examples but I could not find what I looking for. Remember, we are using MVVM pattern and no code behind.
I tried the following codes and have this compile error:
TargetName property cannot be set on a Style Setter
<UserControl.Resources>
<Style x:Name="myTest" TargetType="TextBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="RadioButton.IsChecked" Value="True" TargetName="myRadioButton"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
I read other posts and DataTrigger fix the problem.
<Style x:Name="myTest2" TargetType="RadioButton" >
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=myTextBox}" Value="True">
<Setter Property="IsChecked" Value="True" ></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
Write styles for each RadioButton providing respective TextBox as triggering element. Following is an example for 3 TextBoxes & 3 RadioButtons.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="txtBox0" Grid.Row="0"/>
<TextBox x:Name="txtBox1" Grid.Row="1"/>
<TextBox x:Name="txtBox2" Grid.Row="2"/>
<StackPanel Grid.Row="3" Orientation="Horizontal">
<RadioButton GroupName="grp1" Content="txt1">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox0}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="grp1" Content="txt2">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox1}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
<RadioButton GroupName="grp1" Content="txt3">
<RadioButton.Style>
<Style TargetType="RadioButton">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFocused, ElementName=txtBox2}" Value="True">
<Setter Property="IsChecked" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RadioButton.Style>
</RadioButton>
</StackPanel>
</Grid>
Create a ControlTemplate and add your trigger to ControlTemplate.Triggers
<ControlTemplate.Triggers>
<Trigger Property="HasText" Value="True">
<Setter Property="Visibility" TargetName="LabelText" Value="Hidden" />
</Trigger>
</ControlTemplate.Triggers>
From MSDN:
You can set this property to the name of any element within the scope of where the setter collection (the collection that this setter is part of) is applied. This is typically a named element that is within the template that contains this setter.
TargetName
is mostly used within control templates and not simply within styles like you are attempting to use it. What you can do is to bind your RadioButton
's IsChecked
DP to the IsMouseOver
DP of the TextBox
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With