如何将属性传递给WPF Style

Tal*_*gal 2 wpf

我正在尝试为WPF ItemContainerStyle编写可重用的模板.

此模板更改TabControl项目的外观.此模板旨在用于应用程序中的多个位置.

在每个地方使用它我希望能够传递不同的参数.例如:要更改项目边框的边距:

    <Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">

                <Setter Property="Margin" Value="10,0"/>

                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid SnapsToDevicePixels="true">
                                <Border x:Name="Bd" Width="80"  
                                  Background="Gray" 
                                   Margin="{TemplateBinding Margin}">
                                    <ContentPresenter x:Name="Content" 
                                        ContentSource="Header" />
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
...
<TabControl ItemContainerStyle="{DynamicResource TabItemStyle1}">
Run Code Online (Sandbox Code Playgroud)

在使用样式的地方我想写下这样的东西:

ItemContainerStyle="{DynamicResource TabItemStyle1 Margin='5,0'}"
Run Code Online (Sandbox Code Playgroud)

要么

<TabControl Margin="78,51,167,90" ItemContainerStyle="{DynamicResource TabItemStyle1}"
            ItemContainerStyle.Margin="5,0">
Run Code Online (Sandbox Code Playgroud)

动机是在具有不同边距的不同位置使用此模板.有没有办法做到这一点 ?

谢谢

Tho*_*que 5

您可以使用附加属性来执行此操作.我写了一篇博文,解释了如何做到这一点:

http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/

另一种选择是DynamicResource在派生样式中使用和重新定义资源


Sev*_*ven 5

就我而言,我必须更改应用模板深处的一些参数(因此我不能仅使用设置器)。我不想编写一些遍历可视化树的类或注册附加属性来进行更改。

但是,可以在基本样式中定义资源并在派生定义中覆盖这些值。因此,对于原始示例,它看起来像这样:

<Style x:Key="AbsTabItemStyle" TargetType="{x:Type TabItem}">
    <!-- Override these default values in derived style definitions -->
    <Style.Resources>
        <s:Double x:Key="GridBorderMargin">10</s:Double>
        <Color x:Key="GridBorderColor">Grey</Color>
    </Style.Resources>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid SnapsToDevicePixels="true">
                    <Border x:Name="Bd"
                            Width="80"
                            Background="{DynamicResouces GridBorderColor}"
                            Margin="{DynamicResouces GridBorderMargin}"
                            >
                        <ContentPresenter x:Name="Content" 
                            ContentSource="Header" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="BigMarginTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource AbsTabItemStyle}">
    <!-- Set different values in this derived style definition -->
    <Style.Resources>
        <s:Double x:Key="GridBorderMargin">20</s:Double>
    </Style.Resources>
</Style>

<Style x:Key="RedTabItemStyle" TargetType="{x:Type TabItem}" BasedOn="{StaticResource AbsTabItemStyle}">
    <!-- Set different values in this derived style definition -->
    <Style.Resources>
        <c:Color x:Key="GridBorderColor">Red</Color>
    </Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)