WPF:从ResourceDictionary获取Tag属性

huangapple go评论47阅读模式
英文:

WPF: Get Tag Property from ResourceDictionary

问题

My Button:

<Button Style="{StaticResource ControlBarButton}" Tag="-" />

I want to use Tag property from content of the button
My Resource Dictionary:

<Style TargetType="Button" x:Key="ControlBarButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Button Width="35" Content="我想在这里使用 `Tag` 属性" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
英文:

I'm assigning a text to Tag property of the Button but how can I use it from ResourceDictionary.

My Button:

&lt;Button Style=&quot;{StaticResource ControlBarButton}&quot; Tag=&quot;-&quot;/&gt;

I want to use Tag property from content of the button
My Resource Dictionary:

&lt;Style TargetType=&quot;Button&quot;
       x:Key=&quot;ControlBarButton&quot;&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate&gt;

                &lt;Button Width=&quot;35&quot;
                        Content=&quot;I WANT TO USE `Tag` HERE&quot;&gt;
                &lt;/Button&gt;

            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

答案1

得分: 1

在模板中使用TemplateBinding

<Style TargetType="Button" x:Key="ControlBarButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Button Width="35" Content="{TemplateBinding Tag}">
                </Button>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
英文:

Use a TemplateBinding in the template:

&lt;Style TargetType=&quot;Button&quot; x:Key=&quot;ControlBarButton&quot;&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate&gt;

                &lt;Button Width=&quot;35&quot;
                        Content=&quot;{TemplateBinding Tag}&quot;&gt;
                &lt;/Button&gt;

            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年1月9日 03:02:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定