【问题标题】:WPF trigger to change parent propertyWPF 触发器更改父属性
【发布时间】:2009-11-11 17:13:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: .net wpf xaml triggers parent


    【解决方案1】:

    改成这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 2015-10-20
      • 2011-01-23
      • 2011-11-10
      • 1970-01-01
      • 2011-09-06
      • 1970-01-01
      相关资源
      最近更新 更多