模仿自定义控件中的 ItemsControl 行为

发布于 2024-11-03 07:12:09 字数 976 浏览 1 评论 0原文

我有类 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 技术交流群。

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

发布评论

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

评论(1

迷爱 2024-11-10 07:12:09

您需要绑定的依赖属性,您可以使用 DependencyProperty.AddOwner 来“回收”其他控件的属性,或者创建您自己的属性。此外,您可能希望将绑定从您的属性“转发”到内部 ItemsControl 或您使用的任何内容,例如

<UserControl x:Class="Test.UserControls.MyUserControl3"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             Name="control">
    <Grid>
        <!-- Binds to the new property -->
        <ItemsControl ItemsSource="{Binding ElementName=control, Path=MarkersSource}"/>
    </Grid>
</UserControl>
public partial class MyUserControl3 : UserControl
{
    public static readonly DependencyProperty MarkersSourceProperty =
            DependencyProperty.Register("MarkersSource",
                                        typeof(ObservableCollection<Employee>),
                                        typeof(MyUserControl3),
                                        new UIPropertyMetadata(null));
    public ObservableCollection<Employee> MarkersSource
    {
        get { return (ObservableCollection<Employee>)GetValue(MarkersSourceProperty); }
        set { SetValue(MarkersSourceProperty, value); }
    }

    public MyUserControl3()
    {
        InitializeComponent();
    }
}

使用示例:

<uc:MyUserControl3 MarkersSource="{Binding DpData}">
    <uc:MyUserControl3.Resources>
        <DataTemplate DataType="{x:Type obj:Employee}">
            <TextBlock Text="{Binding Name}" Foreground="Red"/>
        </DataTemplate>
    </uc:MyUserControl3.Resources>
</uc:MyUserControl3>

这里我通过资源隐式应用 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.

<UserControl x:Class="Test.UserControls.MyUserControl3"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             Name="control">
    <Grid>
        <!-- Binds to the new property -->
        <ItemsControl ItemsSource="{Binding ElementName=control, Path=MarkersSource}"/>
    </Grid>
</UserControl>
public partial class MyUserControl3 : UserControl
{
    public static readonly DependencyProperty MarkersSourceProperty =
            DependencyProperty.Register("MarkersSource",
                                        typeof(ObservableCollection<Employee>),
                                        typeof(MyUserControl3),
                                        new UIPropertyMetadata(null));
    public ObservableCollection<Employee> MarkersSource
    {
        get { return (ObservableCollection<Employee>)GetValue(MarkersSourceProperty); }
        set { SetValue(MarkersSourceProperty, value); }
    }

    public MyUserControl3()
    {
        InitializeComponent();
    }
}

Usage example:

<uc:MyUserControl3 MarkersSource="{Binding DpData}">
    <uc:MyUserControl3.Resources>
        <DataTemplate DataType="{x:Type obj:Employee}">
            <TextBlock Text="{Binding Name}" Foreground="Red"/>
        </DataTemplate>
    </uc:MyUserControl3.Resources>
</uc:MyUserControl3>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文