使ToolBar按钮样式适用于工具栏中的UserControls

Ray*_*Ray 6 wpf user-controls toolbar

如何强制ToolBar中的WPF中的UserControl使用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>
Run Code Online (Sandbox Code Playgroud)

我在这些绑定路径的代码后面有一些依赖属性,一切正常.

直到我把它放在ToolBar中.通常,当您将按钮放在工具栏中时,它们会被ToolBar重新设置为工具栏按钮.我已经找到了如何更改此样式(请参阅ToolBar.ButtonStyleKey.但是如何将已定义的样式应用于我的User Control的Button Style依赖项属性?

Yot*_*aXP 20

哦,我的道歉!我误解了你的问题.试一试:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    Hello, world!
</Button>
Run Code Online (Sandbox Code Playgroud)

该样式仅针对按钮,因此它只能应用于按钮.(根据您的评论,这不会成为问题.)