是/否 ToggleButton 的 WPF 模板 [英] WPF template for yes/no ToggleButton
问题描述
谁能帮我提供一个简单的模板,让 WPF ToggleButton 根据切换状态显示是"或否"?
Can anyone help me with a simple template to make a WPF ToggleButton show "yes" or "no" depending on it's toggle state?
我不希望它看起来像一个按钮,只是一段显示是或否的文本.
I dont want it to look like a button though, just a piece of text that either shows as yes or no.
我想要它作为模板,我可以将它作为样式添加到所有现有的 Toggle 控件中.
I'd like it as a template that I can just add as a style to all my existing Toggle controls.
我尝试过使用 Blend,但我是模板新手,不知道该怎么做.
I tried with Blend but I am new to templating and didn't know how to do it.
推荐答案
建立在@Batuu 的回复之上,只留下内容作为文本,只需将样式更新为
Building up on @Batuu's reply, to be just left with the content as text just update the style to
更新
<Style x:Key="OnOffToggleStyle" TargetType="ToggleButton">
<Setter Property="Content" Value="Off"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="On">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
与原回复相比,这里添加的是
Addition here compared to original reply is
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
您基本上将按钮的模板设置为它所包含的内容.
You basically set the template of the button to just be the content that it holds.
这篇关于是/否 ToggleButton 的 WPF 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!