使工具栏按钮样式应用于工具栏中的用户控件

发布于 2024-07-23 07:12:40 字数 1261 浏览 2 评论 0原文

如何强制 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深如许 2024-07-30 07:12:40

哦,抱歉! 我误解了你的问题。 尝试一下:

<Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}">
    Hello, world!
</Button>

该样式仅针对按钮,因此它只能应用于按钮。 (从你的评论来看,这不是问题。)

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.)

独夜无伴 2024-07-30 07:12:40

我找到了一种似乎有效的方法,尽管我对解决此问题的其他方法很感兴趣。

我向我的 UserControl 添加了一个已加载的事件处理程序,并将其放入其中。

if (button.Style == null && this.Parent is ToolBar)
{
    button.Style = (Style)FindResource(ToolBar.ButtonStyleKey);
}

其中,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.

if (button.Style == null && this.Parent is ToolBar)
{
    button.Style = (Style)FindResource(ToolBar.ButtonStyleKey);
}

Where button is the name of the button inside my UserControl.

守不住的情 2024-07-30 07:12:40

您需要一些强大的黑魔法才能让它自动为您的用户控件创建功能齐全的新样式。 您可以添加到工具栏的每种类型的内置控件都有内置样式。 (请注意,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文