为多个元素重用相同的模板

时间:2017-03-01 09:08:29

标签: wpf xaml controltemplate resourcedictionary

我正在我的应用程序中创建按钮样式,但我注意到对于每个按钮我必须一次又一次地重复相同的步骤,我在下面的模板中寻找一种方式,以便我可以传递Path元素的特定属性或者每个按钮的路径不同,但所有其他元素都是相同的,并且顺序相同,我在这里尝试搜索但是找不到任何对我有帮助的特定示例,这里是xaml:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
        <Grid x:Name="MainGrid">
            <Rectangle 
        x:Name="MainRectangle" 
        Fill="#00000000" 
        RadiusX="5" 
        RadiusY="5"/>
            <ContentPresenter 
            x:Name="Presenter" 
            HorizontalAlignment="Center" 
            VerticalAlignment="Center" 
            TextBlock.Foreground="#BB225588"/>
            <Path Name="Minimize" Data="M0,0L32,0 32,8.6899995 0,8.6899995z" Stretch="Fill" Fill="White"  StrokeThickness="2" Width="17" Height="5" VerticalAlignment="Center" HorizontalAlignment="Center"
                Stroke="White" >
            </Path>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Effect">
                    <Setter.Value>
                        <DropShadowEffect ShadowDepth="2" Color="White"  BlurRadius="20"></DropShadowEffect>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="MainRectangle" Property="Fill" Value="{StaticResource ClickEffect}"/>
                <Setter TargetName="Minimize" Property="Stroke" Value="White" />
            </Trigger>

        </ControlTemplate.Triggers>
    </ControlTemplate>

现在在上面的模板中我想要的是一些方法来绑定每个按钮的Path元素属性或某种方式为每个按钮注入不同的Path元素,其余的将保持不变,所以我不想重复同样的风格一次又一次。我怎么能实现它。

1 个答案:

答案 0 :(得分:0)

您可以创建一个自定义Button控件,其中包含您希望能够设置的Path的每个属性的依赖项属性,并使用ControlTemplate绑定到这些属性TemplateBinding标记扩展名,例如:

<强>控制:

public class PathButton : Button
{
    public static readonly DependencyProperty FillProperty =
         DependencyProperty.Register("Fill", typeof(Brush),
         typeof(PathButton), new FrameworkPropertyMetadata(Brushes.White));

    public Brush Fill
    {
        get { return (Brush)GetValue(FillProperty); }
        set { SetValue(FillProperty, value); }
    }
}

<强>模板:

<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type local:PathButton}">
    <Grid x:Name="MainGrid">
        <Rectangle 
                    x:Name="MainRectangle" 
                    Fill="#00000000" 
                    RadiusX="5" 
                    RadiusY="5"/>
        <ContentPresenter 
                    x:Name="Presenter" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center" 
                    TextBlock.Foreground="#BB225588"/>
        <Path Name="Minimize" Data="M0,0L32,0 32,8.6899995 0,8.6899995z" Stretch="Fill"
                      Fill="{TemplateBinding Fill}"
                      StrokeThickness="2" Width="17" Height="5" VerticalAlignment="Center" HorizontalAlignment="Center"
                      Stroke="White" >
        </Path>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect ShadowDepth="2" Color="White"  BlurRadius="20"></DropShadowEffect>
                </Setter.Value>
            </Setter>
        </Trigger>
        <Trigger Property="IsPressed" Value="True">
            <Setter TargetName="MainRectangle" Property="Fill" Value="{StaticResource ClickEffect}"/>
            <Setter TargetName="Minimize" Property="Stroke" Value="White" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<强>用法:

<local:PathButton Content="content" Template="{StaticResource ButtonTemplate}" Fill="Red"  />