在 WPF/XAML 中实现带有图标的工具栏

发布于 2024-09-25 15:29:16 字数 325 浏览 2 评论 0原文

在我的基于桌面的 WPF 应用程序中,我想实现一个具有关键操作(添加、更新、删除等)的工具栏,就像您在任何 Web 界面邮件服务中看到的那样。

为了做到这一点,我有一个大的 PNG 图像,其中包含所有可能的图标(常规、活动、禁用等)。

如何不显示整个图像,而只显示一个区域。例如,如果我的图标是正方形且边长为 50px,则从像素 50 到像素 100?换句话说,如果所有图标都放置到一个大的 PNG 图像中,您建议如何在 WPF 中实现选择图像的一部分以显示在工具栏按钮内?

PS 在网络开发中,通常使用一张包含所有图标的大 PNG 图像,当您想要放置特定图标时,您必须定义要显示的图像区域。

In my desktop-based WPF-application I want to implement a toolbar with key actions (add, update, delete etc.), something like you can see in any web-interface mail-service.

In order to make it I have a large PNG-image with all possible icons (regular, active, disabled, etc.)

How to show not the whole image, but only an area. For example, from pixel 50 to pixel 100 in a case where my icon is square and has 50px side? In other words, how would you suggest to implement in WPF selecting a subsection of an image to display inside of toolbar button if all icons are placed to one big PNG-image?

P.S. In web-development it's common to use one big PNG-image with all icons and when you want to put a specific icon you have to define which area in imagу you want to show.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

划一舟意中人 2024-10-02 15:29:16

这确实不是 WPF 中管理图像资源的方式,如果这样做,您将与平台的自然机制作斗争。您应该考虑拆分图像,然后将每个图像分配给视图中的资源。然后,您可以绑定到 UI 中的每一个,并将其显示为图像或画笔源。

如果您想将它们保留为一个大图像,您可以使用 TileBrush 将它们挑选出来并仅显示您想要的图像区域。

This really isn't the way image resources are meant to be managed in WPF, and you will be fighting the natural mechanisms of the platform if you do it this way. You should consider splitting the images up, and then assigning each one to a resource in the view. You can then bind to each one in the UI and have it appear as either an Image or as the source of a Brush.

If you want to keep them as one large image, you might be able to use a TileBrush to pick them out and display only the region of the image you want.

落墨 2024-10-02 15:29:16

您可能想查看 SourceRect 属性BitmapImage 类(您的图像源)上。

不过我认为 codekaizen 是对的,但这可能不是正确的方法。

You probably want to look at the SourceRect Property on the BitmapImage class (Your image source).

However I think codekaizen is right, this may not be the right approach.

暗恋未遂 2024-10-02 15:29:16

您可以将按钮的背景设置为带有图像的 ImageBrush。将 TileMode 设置为 None,将 ViewboxUnits 设置为 Absolute。您可以将Viewbox设置为适当的区域。然后,您可以绑定到属性以动态更改图像,或者如果它是静态图像,则只需直接设置值。

<Button Width="80" Height="80">
    <Button.Background>
        <ImageBrush ImageSource="..." TileMode="None" ViewboxUnits="Absolute">
            <ImageBrush.Viewbox>
                <Rect Size="80,80" X="{Binding ...}" />
            </ImageBrush.Viewbox>
        </ImageBrush>
    </Button.Background>
</Button>

我同意,这不是最有效地利用资源,但这是可以做到的。

You can set the background of your buttons to an ImageBrush with your image. Set the TileMode to None and ViewboxUnits to Absolute. You could set the Viewbox to the appropriate regions. You could then bind to the properties to dynamically change the image or if it is a static image, just set the values directly.

<Button Width="80" Height="80">
    <Button.Background>
        <ImageBrush ImageSource="..." TileMode="None" ViewboxUnits="Absolute">
            <ImageBrush.Viewbox>
                <Rect Size="80,80" X="{Binding ...}" />
            </ImageBrush.Viewbox>
        </ImageBrush>
    </Button.Background>
</Button>

I agree, it's not the most efficient use of resources, but here's how it can be done.

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