设置地图多边形的样式
我打算为 MapPolygon
创建一个 Style
,但我似乎无法为除了从 继承的属性之外的任何内容添加
,这并没有多大帮助,因为 MapPolygon 实际上并不使用其中的许多属性。Setter
。 code>Control
我主要希望能够设置 Fill
、Stroke
和 StrokeThickness
属性的样式。但是,当我尝试执行此操作时,出现以下错误:“对象引用未设置为对象的实例”。我是否正确地认为这是因为我尝试设置样式的属性不是依赖属性(DependencyProperty
)?
如果我的想法确实正确,那么解决这个问题的最简单方法是创建一个自定义 MapPolygon
控件并为 Fill
、Stroke
创建依赖属性代码> 和<代码>StrokeThickness?
如果我需要澄清一些事情,请告诉我。
更新:
public class StyledMapPolygon : MapPolygon {
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(StyledMapPolygon),
new PropertyMetadata(new SolidColorBrush(), new PropertyChangedCallback(OnFillChanged)));
private static void OnFillChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
StyledMapPolygon instance = (StyledMapPolygon) d;
instance.Fill = e.NewValue as Brush;
}
}
。
<Style x:Key="CustomStyle" TargetType="exts:StyledMapPolygon">
<Setter Property="Fill" Value="{StaticResource BuildingFillBrush}" />
</Style>
这只是我想使用的样式的简化版本。 StyledMapPolygon
是我从 MapPolygon
继承创建的对象。唯一的区别是我为“Fill”创建了一个 DependencyProperty ,它仅映射到基本属性。
上述错误仍然出现在 Setter
中的“Fill”上,但现在可以正常工作(在手机上正确显示)。我可以忍受那里的错误,因为它仍然运行,但我非常希望我的应用程序没有错误。
I was going to create a Style
for MapPolygon
, but I can't seem to add a Setter
for anything other than the properties inherited from Control
, which doesn't help very much since the MapPolygon doesn't actually use many of those properties.
I mainly want to be able to style the Fill
, Stroke
, and StrokeThickness
properties. When I try to do this, however, I get the following error: "Object reference not set to an instance of an object". Am I correct in thinking this is because the properties that I am trying to style are not dependency properties (DependencyProperty
)?
If my thinking here is indeed correct, would the easiest way to solve this problem be to create a custom MapPolygon
control and create dependency properties for Fill
, Stroke
, and StrokeThickness
?
Let me know if I need to clarify something.
Update:
public class StyledMapPolygon : MapPolygon {
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(StyledMapPolygon),
new PropertyMetadata(new SolidColorBrush(), new PropertyChangedCallback(OnFillChanged)));
private static void OnFillChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
StyledMapPolygon instance = (StyledMapPolygon) d;
instance.Fill = e.NewValue as Brush;
}
}
.
<Style x:Key="CustomStyle" TargetType="exts:StyledMapPolygon">
<Setter Property="Fill" Value="{StaticResource BuildingFillBrush}" />
</Style>
This is just a simplified version of a style that I would like to use. the StyledMapPolygon
is a object I created inherited from MapPolygon
. The only difference is that I created a DependencyProperty
for "Fill" that just maps to the base property.
The error mentioned above still appears on "Fill" within the Setter
, but it now works (displays correctly on the phone). I can live with the error there since it still runs, but I would very much like to have my app be error free.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,属性必须是
DependencyProperty
才能由样式设置。添加自己的依赖属性来包装基类上的属性并没有什么问题,但我不建议尝试使用相同的属性名称。创建一个不同名称的属性,并使用属性更改处理程序来中继设置为基础属性的值。
当然,如果您提到的“错误”是智能感知,那么只要代码编译并运行,就真的没有理由关心。
Yes, a property must be a
DependencyProperty
in order to be set by a style.There's nothing wrong with adding your own dependency property that wraps a property on the base class, but I wouldn't recommend trying to use the same property name. Create a differently named property, with property changed handler which relays the value which is set to the underlying property.
Of course if the "error" you mention is Intellisense there's really no reason to care, so long as code compiles and runs.