模仿自定义控件中的 ItemsControl 行为
我有类 Marker
class Marker {
public double Position {get; set;}
public string Label {get; set;}
}
和自定义控件,除其他属性外,它公开了
class MyControl {
public ObservableCollection<Marker> Markers {get; set;}
}
我想模仿 ItemsControl 行为的标记集合,并允许我的组件的用户直接或通过使用 ItemsSource 类比来指定标记。此外,我希望这两种方法都支持数据绑定(最好在 XAML 中)
直接标记
<my:MyControl>
<my:MyControl.Markers>
<my:Marker Position="{Binding X}" />
</my:MyControl.Markers>
</my:MyControl>
使用 MarkersSource 进行标记
<my:MyControl MarkersSource={Binding UserSpecifiedCollection}">
</my:MyControl>
第一种方法非常简单,但我正在努力使用第二种方法。
如何实施 MarkesSource?如何将 UserSpecifiedCollection 的项转换为 Marker 类型? UserSpecifiedCollection 项的属性如何与 Marker 的属性进行数据绑定?
关于转换我认为可以使用 ValueConvertor,但我更喜欢纯 XAML 解决方案,例如 DataTemplates。有可能吗?
I have class Marker
class Marker {
public double Position {get; set;}
public string Label {get; set;}
}
and custom control that, among other properties, exposes collection of markers
class MyControl {
public ObservableCollection<Marker> Markers {get; set;}
}
I'd like to mimic ItemsControl behaviour and allow users of my component to specify markers either directly or by using ItemsSource analogy. Moreover I'd like both methods to support databinding (preferably in XAML)
Markers directly
<my:MyControl>
<my:MyControl.Markers>
<my:Marker Position="{Binding X}" />
</my:MyControl.Markers>
</my:MyControl>
Marker using MarkersSource
<my:MyControl MarkersSource={Binding UserSpecifiedCollection}">
</my:MyControl>
The first method is pretty straightforward but I'am struggling with the second one.
How can I implement MarkesSource? How can be items of UserSpecifiedCollection converted to Marker type? How can be properties of UserSpecifiedCollection items be databound to properties of Marker?
Regards conversion I think a ValueConvertor can be used, but I would prefer pure XAML solution, something like DataTemplates. It is possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要绑定的依赖属性,您可以使用 DependencyProperty.AddOwner 来“回收”其他控件的属性,或者创建您自己的属性。此外,您可能希望将绑定从您的属性“转发”到内部 ItemsControl 或您使用的任何内容,例如
使用示例:
这里我通过资源隐式应用 DataTemplate,但您可以创建另一个属性(重用 ItemsControl.ItemTemplate)并转发到内部 ItemsControl。
You need dependency properties for bindings, you can "recycle" properties of other controls by using
DependencyProperty.AddOwner
or just create your own. Further you probably want to "forward" the binding from your property to the internal ItemsControl or whatever you use, e.g.Usage example:
Here i implicitly apply a DataTemplate via resources, but you can just create another property (reuse ItemsControl.ItemTemplate) and forward that to the internal ItemsControl.