Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger with target name

Tags:

wpf

triggers

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>
like image 295
AustinTX Avatar asked Sep 26 '12 17:09

AustinTX


3 Answers

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>
like image 133
Praboda Avatar answered Nov 12 '22 21:11

Praboda


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>
like image 22
Pale Ale Avatar answered Nov 12 '22 20:11

Pale Ale


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.

like image 1
Tejas Sharma Avatar answered Nov 12 '22 22:11

Tejas Sharma