将 Canvas 设置为控件模板?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您创建画布等类型作为资源时,您将创建该类型的一个实例。这意味着您不能将该资源放置在应用程序中的多个位置(一个元素一次只能位于一个位置)。我认为您应该考虑使用控件模板。
您不需要为此隐藏任何代码。
像这样的事情:
然后在其他地方你做这样的事情:
这将构建一个具有常规外观的按钮,其中包含你的图标作为其内容。该按钮的内容是您的图标。
但是,如果您想完全重新定义按钮的模板,那么您可以这样做
然后在其他地方您可以执行如下操作:
请注意,这对于按钮来说不是一个很好的样式。请参阅 Microsoft 的此示例,了解功能更齐全的按钮的示例模板。
编辑
除非您的
ControlTemplate
中有ContentPresenter
,否则无需将模板分配给内容控件。请注意,任何从Control
派生的类都可以模板化,包括Control
本身。因此,为了将一个项目放入您的视图中,您可以使用:这使用层次结构中最广泛的适用类型,这也是最轻的。
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:
Then elsewhere you do something like this:
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
Then elsewhere you do something like this:
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 yourControlTemplate
, then there's no need to assign the template to a content control. Note that any class derived fromControl
can be templated, includingControl
itself. So in order to place an item into your view, then you can just use:This uses the widest applicable type in the hierarchy, which is also the lightest.
为按钮定义图标的一个好方法是使用DrawingBrush并将其设置为嵌入按钮中的矩形的填充em>:
myDrawingBrush 必须在资源中定义,如下所示:
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:
myDrawingBrush must be defined in resources like this: