【问题标题】:In WPF Style, How can Trigger access a son Element of the Element it is attatched在 WPF 样式中,触发器如何访问它所附加的元素的子元素
【发布时间】:2008-10-09 07:06:47
【问题描述】:

下面是ListBoxItem的Simple Style,ListBoxItem有一个子Border。 Border 有一个值为 8 的 Padding 属性,我想在选择项目时将值更改为 0。如何编写触发器?

    <??Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}" >
                    <Border 
                        SnapsToDevicePixels="True" 
                        HorizontalAlignment="Center" 
                        VerticalAlignment="Center"
                        Padding = "8"
                        Background="{TemplateBinding Background}">
                        <ContentPresenter />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            ??<Setter Property="Padding" Value="0" />??   <----How Can I do this?
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    <??/Style>

【问题讨论】:

    标签: wpf triggers


    【解决方案1】:

    尝试为边框命名(例如,使用 x:Name="border1"),然后使用 Setter 的 TargetName 属性,如下所示:

    <Setter TargetName="border1" Property="Padding" Value="0" />
    

    不确定它是否可以在这样的控件模板中工作,但请试一试。

    【讨论】:

      猜你喜欢
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-05
      相关资源
      最近更新 更多