通过setter v ContentTemplate设置样式内容

时间:2022-08-24 13:08:27

The xaml for the first style works as I want, producing a button with a Wingding glyph using setters to lay out the content and it's properties. The second version of this style is trying to do the same thing but with a DataTemplate for the Content, but it just displays the type of a DataTemplate (ie, System.Windows.DataTemplate).

第一个样式的xaml按我的意愿工作,使用setter生成一个带有Wingding字形的按钮,用于布置内容及其属性。这种风格的第二个版本试图做同样的事情,但内容的DataTemplate,但它只显示DataTemplate的类型(即System.Windows.DataTemplate)。

  1. Why won't the 2nd version display the same content as the 1st?
  2. 为什么第二个版本不会显示与第一个相同的内容?
  3. Assuming the fix is trivial, would one version of the style be preferable over the other for any reason beyond personal preference?
  4. 假设修复是微不足道的,那么除了个人喜好之外,这种风格的一个版本是否会优于另一个版本?

NOTE: I am showing the bindings and triggers in case there is something in there that is affecting the content, but it's only the the first part of the style that varies

注意:我正在显示绑定和触发器,以防有影响内容的东西,但它只是样式的第一部分变化

Cheers,
Berryl

干杯,Berryl

Style 1

Displays: 通过setter v ContentTemplate设置样式内容

显示:

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}" >
    <Setter Property="Content" Value="a" />
    <Setter Property="Foreground" Value="Navy" />
    <Setter Property="FontFamily" Value="Wingdings 3" />
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="FontSize" Value="18" />
    <Setter Property="Width" Value="30" />
    <Setter Property="Height" Value="Auto" />

    <!--What makes it an Edit button-->
    <Setter Property="Command" Value="{Binding ActivateThisSatelliteVmCommand}"/>
    <Setter Property="ToolTip">
        <Setter.Value>
            <TextBlock>
                <TextBlock.Text>
                    <Binding Path="HeaderLabel" StringFormat="{resx:Resx ResxName=Smack.Core.Presentation.Resources.MasterDetail, Key=Item_Edit_Label}"/>
                </TextBlock.Text>
            </TextBlock>
        </Setter.Value>
    </Setter>

    <!-- WHen its available -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="theBorder" CornerRadius="4">
                    <ContentPresenter x:Name="theContent" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter TargetName="theContent" Property="Visibility" Value="Hidden"/>
                        <Setter TargetName="theBorder" Property="Background" Value="Transparent"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="theContent" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="theContent" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="theBorder" Property="Background" Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Style 2

Displays "System.Windows.DataTemplate"

显示“System.Windows.DataTemplate”

<Style x:Key="EditCommandButtonStyle" TargetType="{x:Type Button}" >
    <Setter Property="Content">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="a" FontFamily="Wingdings 3" FontWeight="Bold" FontSize="18" Foreground="Navy" />
            </DataTemplate>
        </Setter.Value>
    </Setter>

    <!--What makes it an Edit button-->
    <Setter Property="Command" Value="{Binding ActivateThisSatelliteVmCommand}"/>
    <Setter Property="ToolTip">
        <Setter.Value>
            <TextBlock>
                <TextBlock.Text>
                    <Binding Path="HeaderLabel" StringFormat="{resx:Resx ResxName=Core.Presentation.Resources.MasterDetail, Key=Item_Edit_Label}"/>
                </TextBlock.Text>
            </TextBlock>
        </Setter.Value>
    </Setter>

    <!-- When its available -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="theBorder" CornerRadius="4">
                    <ContentPresenter x:Name="theContent" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="False">
                        <Setter TargetName="theContent" Property="Visibility" Value="Hidden"/>
                        <Setter TargetName="theBorder" Property="Background" Value="Transparent"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="theContent" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="theContent" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="theBorder" Property="Background" Value="Orange"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个解决方案

#1


15  

Your Content property is set to a DataTemplate

您的Content属性设置为DataTemplate

DataTemplates are meant to be used with Template properties, and not directly inserted into the VisualTree via the Content property

DataTemplates旨在与Template属性一起使用,而不是通过Content属性直接插入到VisualTree中

Change your Style Setter to set the ContentTemplate instead of Content and it should work fine

更改样式设置器以设置ContentTemplate而不是内容,它应该可以正常工作

<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <TextBlock Text="a" FontFamily="Wingdings 3" FontWeight="Bold" FontSize="18" Foreground="Navy" />
        </DataTemplate>
    </Setter.Value>
</Setter>

As for your second question, I prefer the first because its simpler, and I think it may contain fewer elements in the Visual Tree (I'd have to double-check that)

至于你的第二个问题,我更喜欢第一个,因为它更简单,我认为它可能包含更少的视觉树中的元素(我必须仔细检查)

#1


15  

Your Content property is set to a DataTemplate

您的Content属性设置为DataTemplate

DataTemplates are meant to be used with Template properties, and not directly inserted into the VisualTree via the Content property

DataTemplates旨在与Template属性一起使用,而不是通过Content属性直接插入到VisualTree中

Change your Style Setter to set the ContentTemplate instead of Content and it should work fine

更改样式设置器以设置ContentTemplate而不是内容,它应该可以正常工作

<Setter Property="ContentTemplate">
    <Setter.Value>
        <DataTemplate>
            <TextBlock Text="a" FontFamily="Wingdings 3" FontWeight="Bold" FontSize="18" Foreground="Navy" />
        </DataTemplate>
    </Setter.Value>
</Setter>

As for your second question, I prefer the first because its simpler, and I think it may contain fewer elements in the Visual Tree (I'd have to double-check that)

至于你的第二个问题,我更喜欢第一个,因为它更简单,我认为它可能包含更少的视觉树中的元素(我必须仔细检查)