Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF trigger to change parent property

I want to override element triggers so, when clicking on a textbox, it changes the property of the parent border.

But the parent targetname is not recognized. Here is the code sample:

   <Style x:Key="customstyle" TargetType="{x:Type local:customcontrol}">
    <Setter Property="Background" Value="{StaticResource DownGradientBrush}"/>
    <Setter Property="BorderBrush" Value="{StaticResource DownGradientBorder}"/>
    <Setter Property="Foreground" Value="{StaticResource TextBoxForeground}"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:customcontrol}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="15"/>
                        </Grid.ColumnDefinitions>
                        <Border Grid.Column="0" 
                                Grid.RowSpan="2"
                                HorizontalAlignment="Stretch">
    <TextBox x:Name="TextBox"
             Grid.Column="0" 
             Grid.RowSpan="2"
             Text="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}, Path=Value}"
             Width="Auto"
             Height="Auto">
        <TextBox.Style>
            <Style BasedOn="{StaticResource StyleTextBox}" TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="Bd" 
                                Property="Background" 
                                Value="{StaticResource CustomGradientBorder}" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True" />
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
    </Border>
    </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsFocused" Value="True">
            <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource CustomGradientBorder}" />
        </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>
like image 459
immuner Avatar asked Jan 24 '23 00:01

immuner


1 Answers

change it like this:

<ControlTemplate.Triggers>
  <Trigger Property="IsFocused" Value="True" SourceName="TextBox">
    <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource CustomGradientBorder}" />
  </Trigger>
</ControlTemplate.Triggers>
like image 106
viky Avatar answered Jan 30 '23 11:01

viky