wpf 如何覆盖 ControlTemplate 中的属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14217372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:57:19  来源:igfitidea点击:

How do I override a property in a ControlTemplate?

c#wpf

提问by Hackworth

I have the following XAML:

我有以下 XAML:

<Window x:Class="WpfTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="227" Width="391">
    <Window.Resources>
        <ControlTemplate x:Key="PentagonButton" TargetType="{x:Type Button}">
            <Grid>
                <Path Data="M 0,-1 L -.95,-.3 L -.58,.8 L .58,.8 L .95,-.3 Z" Stroke="Black" Stretch="Uniform" Fill="Red" />
                <Viewbox>
                    <ContentPresenter Margin="20" />
                </Viewbox>
            </Grid>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <Button Content="red" Margin="12,19,0,0" Template="{StaticResource PentagonButton}" HorizontalAlignment="Left" Width="166" Height="151" VerticalAlignment="Top" />        
        <Button Content="blue" Margin="178,19,0,0" Template="{StaticResource PentagonButton}" Height="151" VerticalAlignment="Top" HorizontalAlignment="Left" Width="173" />        
    </Grid>
</Window>

Because of Fill="Red", obviously all buttons will have a red fill/background color. I want to use this template for several buttons, but each of them with a different fill color, known at design time. Setting the background color has no effect, so I have to change the Fill color, but I can't define the Path property again in the actual button definitions. How do I access and override this property in the XAML?

由于 Fill="Red",显然所有按钮都将具有红色填充/背景颜色。我想将这个模板用于几个按钮,但每个按钮都有不同的填充颜色,在设计时就知道了。设置背景色没有效果,所以我必须改变Fill颜色,但我不能在实际按钮定义中再次定义Path属性。如何在 XAML 中访问和覆盖此属性?

回答by Joachim Kerschbaumer

Don′t use the template directly. Assign a Style and use that.

不要直接使用模板。分配一个样式并使用它。

i.e.

IE

 <Style x:Key="PentagonButton" TargetType="{x:type Button}">
 <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Path Data="M 0,-1 L -.95,-.3 L -.58,.8 L .58,.8 L .95,-.3 Z" Stroke="Black" Stretch="Uniform" Fill="{TemplateBinding Background}" />
                    <Viewbox>
                        <ContentPresenter Margin="20" />
                    </Viewbox>
                </Grid>
            </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>

Then use it like this:

然后像这样使用它:

<Grid>
        <Button Content="red" Margin="12,19,0,0" Style="{StaticResource PentagonButton}" HorizontalAlignment="Left" Width="166" Height="151" VerticalAlignment="Top" />        
        <Button Content="blue" Margin="178,19,0,0" Style="{StaticResource PentagonButton}" Height="151" VerticalAlignment="Top" HorizontalAlignment="Left" Width="173" Background="Blue" />        
    </Grid>

回答by Florian Gl

Use TemplateBinding to change the values of your templates in the xaml of its controls:

使用 TemplateBinding 在其控件的 xaml 中更改模板的值:

                <ControlTemplate x:Key="PentagonButton" TargetType="{x:Type Button}">
                    <Grid>
                        <Path Data="M 0,-1 L -.95,-.3 L -.58,.8 L .58,.8 L .95,-.3 Z" Stroke="Black" Stretch="Uniform" Fill="{TemplateBinding Background}" />
                        <Viewbox>
                            <ContentPresenter Margin="20" />
                        </Viewbox>
                    </Grid>
                </ControlTemplate>

Your button:

你的按钮:

                <Button Background="Red" Content="red" Margin="12,19,0,0" Template="{StaticResource PentagonButton}" HorizontalAlignment="Left" Width="166" Height="151" VerticalAlignment="Top" />
                <Button Background="Blue" Content="blue" Margin="178,19,0,0" Template="{StaticResource PentagonButton}" Height="151" VerticalAlignment="Top" HorizontalAlignment="Left" Width="173" />