使工具栏按钮样式应用于工具栏中的用户控件
如何强制 WPF 中 ToolBar 内的 UserControls 使用 ToolBar 的默认按钮样式?
我有一个名为 ImageButton 的简单用户控件,它具有类似这样的 XAML:
<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>
我在这些绑定路径后面的代码中有一些依赖属性,并且一切正常。
直到我将它放入工具栏中。 通常,当您将按钮放入工具栏中时,工具栏会重新调整它们的样式,使其看起来像工具栏按钮。 我已经找到了如何更改这种样式(请参阅 ToolBar.ButtonStyleKey。但是如何将已定义的样式应用到用户控件的按钮样式依赖属性呢?
How do I force UserControls in WPF that are inside a ToolBar to use the ToolBar's default button style?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
哦,抱歉! 我误解了你的问题。 尝试一下:
该样式仅针对按钮,因此它只能应用于按钮。 (从你的评论来看,这不是问题。)
Ooh, my apologies! I misinterpreted your question. Give this a shot:
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.)
我找到了一种似乎有效的方法,尽管我对解决此问题的其他方法很感兴趣。
我向我的 UserControl 添加了一个已加载的事件处理程序,并将其放入其中。
其中,button 是我的 UserControl 内按钮的名称。
I found a way, that seems to work, although I'm interested in other ways of solving this.
I added a loaded event handler to my UserControl and put this inside it.
Where button is the name of the button inside my UserControl.
您需要一些强大的黑魔法才能让它自动为您的用户控件创建功能齐全的新样式。 您可以添加到工具栏的每种类型的内置控件都有内置样式。 (请注意,ToolBar.ButtonStyleKey 并不孤单。)您需要自己制作并应用与标准工具栏元素相匹配的自己的样式。
You would require some powerful black magic in order to get it to create a functioning new style for your usercontrol automatically. There are built-in styles for each type of built-in control that you may add to the toolbar. (Notice that ToolBar.ButtonStyleKey is not alone.) You'll need to craft and apply your own style that matches the standard toolbar elements yourself.