这个ContentTemplate有什么问题吗?

时间:2022-09-13 17:04:57

I'm getting an error for this content template within a style: "Must specify both Property and Value for Setter." Aren't I doing that?

我在样式中为这个内容模板获取一个错误:“必须指定Setter的属性和值。”难道我这样做吗?

<Style x:Key="LinkButton" TargetType="Button">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <ContentPresenter/>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <Label x:Name="ContentRoot">
                <StackPanel Orientation="Horizontal">
                    <Viewbox Width="24" Height="24" VerticalAlignment="Center">
                        <Image x:Name="ButtonImage" Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Tag}" />
                    </Viewbox>
                    <TextBlock VerticalAlignment="Center" x:Name="ButtonText" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=Content}"></TextBlock>
                </StackPanel>
            </Label>
            <DataTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter TargetName="ButtonText" Property="TextBlock.TextDecorations" Value="Underline"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Setter.Value>
</Setter>

And here is a button that will be using this style:

这里有一个按钮将使用这个样式:

<Button Name="HelpButton" Style="{StaticResource LinkButton}" Height="30" Content="Help" Tag="Help.png"/>

Thanks!

谢谢!

1 个解决方案

#1


0  

I have loaded this up and have no such problems. The only problem I see is that your button will never have the style applied. This is because if you want the style applied you need to remove the x:Key from the style. Otherwise if you want the style applied only to the HelpButton, the definition should look like this:

我已经加载了这个,没有这样的问题。我看到的唯一问题是,您的按钮永远不会应用样式。这是因为如果希望应用样式,需要从样式中删除x:Key。否则,如果您想要只应用于HelpButton的样式,则定义应该如下所示:

<Button Name="HelpButton" Style="{StaticResource LinkButton}" Height="30" Content="Help" Tag="Help.png"/>

But I cannot see the error you are seeing.

但我看不到你看到的错误。

#1


0  

I have loaded this up and have no such problems. The only problem I see is that your button will never have the style applied. This is because if you want the style applied you need to remove the x:Key from the style. Otherwise if you want the style applied only to the HelpButton, the definition should look like this:

我已经加载了这个,没有这样的问题。我看到的唯一问题是,您的按钮永远不会应用样式。这是因为如果希望应用样式,需要从样式中删除x:Key。否则,如果您想要只应用于HelpButton的样式,则定义应该如下所示:

<Button Name="HelpButton" Style="{StaticResource LinkButton}" Height="30" Content="Help" Tag="Help.png"/>

But I cannot see the error you are seeing.

但我看不到你看到的错误。