.net - WPF触发器更改父属性

标签 .net wpf xaml triggers parent

我想覆盖元素触发器,因此,当单击文本框时,它会更改父边框的属性。

但是无法识别父目标名称。这是代码示例:

   <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>

最佳答案

像这样改变它:

<ControlTemplate.Triggers>
  <Trigger Property="IsFocused" Value="True" SourceName="TextBox">
    <Setter TargetName="Bd" Property="BorderBrush" Value="{StaticResource CustomGradientBorder}" />
  </Trigger>
</ControlTemplate.Triggers>

关于.net - WPF触发器更改父属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1716677/

相关文章:

.net - Entity Framework 5 : Proper method to replace entry in context

.net - 视觉设计应用 : WPF or WinForms(GDI)?

WPF:在用户控件中设置键盘焦点? (按键绑定(bind)问题)

.net - VS XAML设计器如何知道如何自动填充某些值?

C# 是否有异常概述?

.net - 如何构建附加到 .NET 进程并监听特定类型事件的自定义调试器?

c# - .NET 远程处理,为什么列表不可远程处理?

WPF 停止 ListView ScrollBar 触发点击

c# - UWP TextBox puts\r only - 如何设置 LineBreak

c# - 为什么 wpf UpdateSourceTrigger 没有明确绑定(bind)?