在样式中触发以在ControlTemplate中设置控件的属性

时间:2011-11-16 17:17:33

标签: .net wpf triggers

我现在拥有的:

<Style TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
    <Style.Triggers>
        <Trigger Property="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
            <Setter TargetName="WrapPanelItem" Property="ItemHeight" Value="100" />
            <Setter TargetName="WrapPanelItem" Property="ItemWidth" Value="400" />
        </Trigger>
    </Style.Triggers>

    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}" >
                <WrapPanel Name="WrapPanelItem" Margin="5" IsItemsHost="True" Orientation="Horizontal"
                    ItemHeight="{Binding Value, ElementName=ZoomSlider }"
                    ItemWidth="{Binding Value, ElementName=ZoomSlider }"
                    VerticalAlignment="Top" HorizontalAlignment="Stretch" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我最接近的是将名称添加到WrapPanel以尝试从其上方的Trigger引用,但无法找到TargetName。我也尝试直接创建一个附加到WrapPanel的单独样式,但是这导致了使用PhotoListBoxStyle的控件问题,它是它的父级:

<Style x:Key="WrapPanelSetter" TargetType="{x:Type WrapPanel}">
    <Setter Property="ItemHeight" Value="{Binding Value, ElementName=ZoomSlider }" />
    <Setter Property="ItemWidth" Value="{Binding Value, ElementName=ZoomSlider }" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="Margin" Value="5" />

    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
            <Setter Property="ItemHeight" Value="100" />
            <Setter Property="ItemWidth" Value="400" />
        </DataTrigger>
    </Style.Triggers>
</Style>

感谢您的帮助。如果有更好的选择,我非常愿意尝试不同的方法!

2 个答案:

答案 0 :(得分:1)

我可以使用ControlTemplate中的ControlTemplate.Triggers来添加DataTrigger,其中包含ControlTemplate中创建的控件的TargetName:

<Style TargetType="{x:Type ListBox}" x:Key="PhotoListBoxStyle">
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBox}" >
                <WrapPanel Name="WrapPanelItem" Margin="5" IsItemsHost="True" Orientation="Horizontal"
                    ItemHeight="{Binding Value, ElementName=ZoomSlider }"
                    ItemWidth="{Binding Value, ElementName=ZoomSlider }"
                    VerticalAlignment="Top" HorizontalAlignment="Stretch" />
                <ControlTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
                        <Setter TargetName="WrapPanelItem" Property="ItemHeight" Value="100" />
                        <Setter TargetName="WrapPanelItem" Property="ItemWidth" Value="400" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

WrapPanel样式,将Trigger更改为DataTrigger

触发器应基于当前UIElement的属性,而DataTriggers则基于对其他对象的绑定

<Style x:Key="WrapPanelSetter" TargetType="{x:Type WrapPanel}">
    <Setter Property="ItemHeight" Value="{Binding Value, ElementName=ZoomSlider }" />
    <Setter Property="ItemWidth" Value="{Binding Value, ElementName=ZoomSlider }" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Orientation" Value="Horizontal" />
    <Setter Property="Margin" Value="5" />

    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsChecked, ElementName=DetailView}" Value="False">
            <Setter Property="ItemHeight" Value="100" />
            <Setter Property="ItemWidth" Value="400" />
        </DataTrigger >
    </Style.Triggers>
</Style>