英文:
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:
<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="I WANT TO USE `Tag` HERE">
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案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:
<Style TargetType="Button" x:Key="ControlBarButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Width="35"
Content="{TemplateBinding Tag}">
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论