wpf - 如何在 XAML 中更改 ToggleButton 的自定义内容

标签 wpf xaml togglebutton

我更改了 ToggleButton 的样式在 ResourceDictionary .以我的风格,我正在更改 ControlTemplateToggleButton我在 Content 之前放置了一张图片.

现在这是我的问题:图片需要换不同的ToggleButtons我在 XAML 中使用,我应该为每个 ToggleButton 定义不同的样式吗?使用不同的图像还是有什么方法可以在 XAML 中更改其图像?

<Style x:Key="BaseToggleButton" TargetType="ToggleButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <StackPanel Orientation="Horizontal" x:Name="Border">
                    <Image Width="13" Height="13" Source="{StaticResource ColumnsLayoutMiniIcon}"/>
                    <TextBlock>
                        <ContentPresenter/>
                    </TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

有没有办法可以在这里更改图像而不是样式?
<RadioButton Content="Plan View"
             GroupName="View"
             Style="{StaticResource BaseToggleButton}">
</RadioButton>

最佳答案

当然,您可以利用一个未使用的属性,它在这种称为 Tag 的情况下非常方便。一旦我们将其绑定(bind)到模板,您可以使用它来传递图像路径或资源声明等;

<Style x:Key="BaseToggleButton" TargetType="ToggleButton">
    <!-- Let's give it a default -->
    <Setter Property="Tag" Value="{StaticResource ColumnsLayoutMiniIcon}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <StackPanel Orientation="Horizontal" x:Name="Border">
                    <Image Width="13" Height="13" 
                           Source="{TemplateBinding Tag}"/>
                    <ContentPresenter/>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我们可以保持原样,它应该将该迷你图标显示为您的默认图标,您可以在实例级别指定一个新图标,例如;
<ToggleButton Content="Plan View"
              Tag="{StaticResource ADifferentImagePathOrResourcePointingToOne}"
              Style="{StaticResource BaseToggleButton}"/>

希望这会有所帮助,干杯。

关于wpf - 如何在 XAML 中更改 ToggleButton 的自定义内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27809508/

相关文章:

wpf - 在 WPF 中显示大文本的最佳方式?

c# - AxisAngleRotation3D.Axis 属性的真正含义是什么?

WPF 绑定(bind) FallbackValue 设置为绑定(bind)

visual-studio - 可以在 VS 2008 中默认禁用 XAML 设计器吗?

wpf - 设置过滤器后如何更新 RadGridView?

wpf - 在 ComboBoxItem 中设置文本颜色

安卓布局 : Why don't the TextView and a ToggleButton align

c# - 按钮和数据触发器不起作用

javascript - 提交表单数据的 toggle() div 元素

python - 是否有内置方法来获取 ToggleButton 组的当前选择?