使 ToolBar Button Style 应用到 Toolbar 中的 UserControls [英] Making the ToolBar Button Style apply to UserControls in the Toolbar
问题描述
如何强制 WPF 中 ToolBar 内的 UserControls 使用 ToolBar 的默认按钮样式?
How do I force UserControls in WPF that are inside a ToolBar to use the ToolBar's default button style?
我有一个名为 ImageButton 的简单用户控件,它具有类似这样的 XAML:
I have a simple user control called ImageButton which has XAML something like this:
<UserControl x:Class="myNameSpace.ImageButtonUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="imageButton"
>
<Button Style="{Binding ElementName=imageButton, Path=ButtonStyle}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding ElementName=imageButton, Path=ImageSource}"
VerticalAlignment="Center"
/>
<TextBlock Text="{Binding ElementName=imageButton, Path=Text}"
Style="{Binding ElementName=imageButton, Path=TextStyle}"
VerticalAlignment="Center"
/>
</StackPanel>
</Button>
</UserControl>
我在这些绑定路径的代码背后有一些依赖属性,一切正常.
I have some dependency properties in the code behind for those binding paths and everything works fine.
直到我把它放在一个工具栏中.通常,当您将按钮放入 ToolBar 时,它们会被 ToolBar 重新样式化,使其看起来像工具栏按钮.我已经找到了如何更改此样式(请参阅 ToolBar.ButtonStyleKey.但是我如何将已经定义的样式应用到我的用户控件的按钮样式依赖属性?
That is until I put it in a ToolBar. Normally when you put buttons in a ToolBar they are restyled by the ToolBar to look like toolbar buttons. I've found out how I can change this style (see ToolBar.ButtonStyleKey. But how can I apply the already defined style to my User Control's Button Style dependency property?
推荐答案
哦,抱歉!我误解了你的问题.试一试:
Ooh, my apologies! I misinterpreted your question. Give this a shot:
<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
Hello, world!
</Button>
该样式仅针对按钮,因此只能应用于按钮.(从您的评论来看,这不会成为问题.)
The style only targets buttons, so it can only be applied to buttons. (Which won't be a problem by the look of your comment.)
这篇关于使 ToolBar Button Style 应用到 Toolbar 中的 UserControls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!