[英]Using XAML with custom control
I have a custom WPF control derived from Control and some Path XAML source code: 我有一个从Control和一些Path XAML源代码派生的自定义WPF控件:
<Path Width="16" Height="16" Margin="6,0,0,0" x:Key="CheckMark" SnapsToDevicePixels="False" Stroke="Black" Fill="Black" StrokeThickness="1"
Data="M 12.4227,0.00012207C 12.4867,0.126587 12.5333,0.274536
12.6787,0.321411C 9.49199,3.24792 6.704,6.57336
4.69865,10.6827C 4.04399,11.08 3.47066,11.5573 2.83199,
11.9706C 2.09467,10.2198 1.692,8.13196 3.8147e-006,
7.33606C 0.500004,6.79871 1.31733,6.05994 1.93067,6.2428C
2.85999,6.51868 3.14,7.9054 3.60399,8.81604C 5.80133,
5.5387 8.53734,2.19202 12.4227,0.00012207 Z " />
that Path should be used in control's OnRender to draw some check mark. 应该在控件的OnRender中使用该Path来绘制一些复选标记。
The question is where should I put this XAML Path? 问题是我应该把这个XAML路径放在哪里? My custom control does not have its own XAML file, but it has default style declared in Generic.xaml
我的自定义控件没有自己的XAML文件,但具有在Generic.xaml中声明的默认样式
<Style TargetType="{x:Type layers:TargetsGrid}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type layers:TargetsGrid}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Obviously, I can add a dependency property of type Path to the control and assign it in default style, but is there some easier way? 显然,我可以将Path类型的依赖项属性添加到控件中,并以默认样式进行分配,但是有没有更简单的方法? Probably I should place my Path to some separate XAML file?
可能我应该将Path放置到一些单独的XAML文件中?
Why not put it within the border? 为什么不把它放在边界内?
<ControlTemplate TargetType="{x:Type layers:TargetsGrid}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Path ... />
</Border>
</ControlTemplate>
You can create a separate ResourceDictionary and place your Path in that, and do something like: 您可以创建一个单独的ResourceDictionary并将其放在其中,然后执行以下操作:
//Pseudo code, not tested
var resourcs = new ResourceDictionary(new Uri("path to your ResourceDictionary"))
var path = (Path)resources["MyPath"];
Or you can create your Path entirely in code if you don't plan to reuse it. 或者,如果您不打算重复使用它,则可以完全在代码中创建Path。
Or you can keep it in Generic.xaml alongside the ControlTemplate, I don't really see an issue in doing that. 或者,您可以将其与ControlTemplate一起保存在Generic.xaml中,这样做的确没有什么问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.