我更改了 ToggleButton
的样式在 ResourceDictionary
.以我的风格,我正在更改 ControlTemplate
的ToggleButton
我在 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/