将 Canvas 设置为控件模板?

发布于 2024-08-08 04:51:24 字数 550 浏览 3 评论 0原文

我在 ResourceDictionary xaml 文件中有一个 Canvas,如下所示:

<Canvas x:Key="Icon">
     <Path ... />
     <Path ... />
</Canvas>

在我的代码隐藏中,我使用加载此图标

LayoutGrid.Children.Add(FindResource("Icon") as Canvas);

这工作正常。现在我想创建一个使用与模板相同的图标的按钮。因此,我创建了一个控件模板:

<ControlTemplate x:Key="IconTemplate">
    ...
</ControlTemplate>

现在问题是:如何将“图标”资源画布放入控件模板中?据我所知,Canvas 没有 Style 或 Template 属性。它有一个 Children 属性,但无法通过 XAML 访问。我将如何在模板中使用我的画布?

I have a Canvas in a ResourceDictionary xaml file like so:

<Canvas x:Key="Icon">
     <Path ... />
     <Path ... />
</Canvas>

In my code-behind I load this icon using

LayoutGrid.Children.Add(FindResource("Icon") as Canvas);

And this works fine. Now I want to create a button that uses the same icon as a template. So I create a control template:

<ControlTemplate x:Key="IconTemplate">
    ...
</ControlTemplate>

Now here's the problem: How would I put the "Icon" resource canvas into the control template? As far as I know, Canvas does not have a Style or Template property. It has a Children property, but it's not accessible via XAML. How would I go about using my canvas in a template?

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

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

发布评论

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

评论(2

少跟Wǒ拽 2024-08-15 04:51:24

当您创建画布等类型作为资源时,您将创建该类型的一个实例。这意味着您不能将该资源放置在应用程序中的多个位置(一个元素一次只能位于一个位置)。我认为您应该考虑使用控件模板。

您不需要为此隐藏任何代码。

像这样的事情:

<ControlTemplate x:Key="Icon">
  <Canvas>
   <Path ... />
   <Path ... />
  </Canvas>
</ControlTemplate>

然后在其他地方你做这样的事情:

<Button>
  <Control Template="{StaticResource Icon}" />
</Button>

这将构建一个具有常规外观的按钮,其中包含你的图标作为其内容。该按钮的内容是您的图标。

但是,如果您想完全重新定义按钮的模板,那么您可以这样做

<ControlTemplate x:Key="Icon" TargetType="Button">
  <Canvas>
   <Path ... />
   <Path ... />
  </Canvas>
</ControlTemplate>

然后在其他地方您可以执行如下操作:

<Button Template="{StaticResource Icon}" />

请注意,这对于按钮来说不是一个很好的样式。请参阅 Microsoft 的此示例,了解功能更齐全的按钮的示例模板。

编辑

除非您的 ControlTemplate 中有 ContentPresenter,否则无需将模板分配给内容控件。请注意,任何从 Control 派生的类都可以模板化,包括 Control 本身。因此,为了将一个项目放入您的视图中,您可以使用:

<Control Template="{StaticResource Icon}" />

这使用层次结构中最广泛的适用类型,这也是最轻的。

When you create a type such as canvas as a resource, then you are creating ONE instance of the type. This means that you cannot place that resource in more than one location in your application (an element can only be in one place at a time). You should look at using control templates, I think.

You don't need any code behind for this.

Something like this:

<ControlTemplate x:Key="Icon">
  <Canvas>
   <Path ... />
   <Path ... />
  </Canvas>
</ControlTemplate>

Then elsewhere you do something like this:

<Button>
  <Control Template="{StaticResource Icon}" />
</Button>

This constructs a regular looking button with your icon as it's content. The content of that button is your icon.

If, however, you want to completely redefine the template of your button, then you would do so

<ControlTemplate x:Key="Icon" TargetType="Button">
  <Canvas>
   <Path ... />
   <Path ... />
  </Canvas>
</ControlTemplate>

Then elsewhere you do something like this:

<Button Template="{StaticResource Icon}" />

Note that this isn't a great style for a button. Look at this example from Microsoft for an example of a more fully featured button template.

EDIT

Unless you have a ContentPresenter in your ControlTemplate, then there's no need to assign the template to a content control. Note that any class derived from Control can be templated, including Control itself. So in order to place an item into your view, then you can just use:

<Control Template="{StaticResource Icon}" />

This uses the widest applicable type in the hierarchy, which is also the lightest.

勿忘初心 2024-08-15 04:51:24

为按钮定义图标的一个好方法是使用DrawingBrush并将其设置为嵌入按钮中的矩形的填充em>:

<Button>
    <Rectangle
        Width="32"
        Height="32"
        Fill={Background myDrawingBrush}
    />
</Button>

myDrawingBrush 必须在资源中定义,如下所示:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
    <DrawingBrush x:Key="dialogerror" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing>
                    ... define geometry here ...
                </GeometryDrawing>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</ResourceDictionary>

A good way to do define an icon for a button is to use a DrawingBrush and set it as the fill of a Rectangle that you embed in the Button:

<Button>
    <Rectangle
        Width="32"
        Height="32"
        Fill={Background myDrawingBrush}
    />
</Button>

myDrawingBrush must be defined in resources like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
    <DrawingBrush x:Key="dialogerror" Stretch="Uniform">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing>
                    ... define geometry here ...
                </GeometryDrawing>
        </DrawingBrush.Drawing>
    </DrawingBrush>
</ResourceDictionary>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文