简体   繁体   English

如何使用自己的DataTemplate DependencyProperty实现WPF控件?

[英]How do I implement a WPF control with its own DataTemplate DependencyProperty?

I am an intermediate WPF developer, with working knowledge on how to implement dependency properties as well as simple custom controls. 我是一名中级WPF开发人员,具有如何实现依赖项属性以及简单自定义控件的工作知识。 I do not yet understand how I can add a DataTemplate dependency property to a custom control, and use it to define the element tree for each datum in a collection of data. 我还不知道如何将DataTemplate依赖项属性添加到自定义控件,并使用它来为数据集合中的每个数据定义元素树。

The full story is that I have been working on creating a WPF map control that displays many different points and geometric shapes on the map, over map tiles. 完整的故事是我一直在创建一个WPF地图控件,它在地图上通过地图图块显示许多不同的点和几何形状。 These shapes will translate with the rest of the map when user "drags" the map around. 当用户“拖动”地图时,这些形状将与地图的其余部分一起转换。

I have accomplished this, insofar that I have created the map control, and can add child elements to it in Xaml that have map coordinates. 我已经完成了这个,因为我已经创建了地图控件,并且可以在Xaml中添加具有地图坐标的子元素。 I would like to take this farther, and add properties for collections of data, ie points, areas, etc. To better understand what I'm looking for, I would like to re-create two properties from ListBox: ItemsSource and ItemTemplate. 我想更进一步,并添加数据集合的属性,即点,区域等。为了更好地理解我正在寻找的东西,我想从ListBox重新创建两个属性:ItemsSource和ItemTemplate。

I have added two dependency properties to my Map control - PointsSource and PointsTemplate. 我已经为我的Map控件添加了两个依赖项属性 - PointsSource和PointsTemplate。 PointsSource is of type IEnumberable and represents the collection of data to display on the map. PointsSource的类型为IEnumberable,表示要在地图上显示的数据集合。 PointsTemplate represents what each of those datum should look like. PointsTemplate表示每个数据应该是什么样子。 Simply throwing these properties into my control is obviously not enough, but I am unsure of how to coordinate them with one another. 简单地将这些属性抛入我的控制中显然是不够的,但我不确定如何将它们相互协调。 If anyone has working knowledge of creating a custom data control with it's own DataTemplate properties for changing the UI tree for each data element, I would really appreciate it. 如果任何人都有使用它自己的DataTemplate属性创建自定义数据控件的工作知识来更改每个数据元素的UI树,我真的很感激。

I have found what I am looking for in the DataTemplate itself. 我在DataTemplate本身找到了我要找的东西。 The DataTemplate provides a function for code behind called LoadContent(). DataTemplate为后面的代码提供了一个名为LoadContent()的函数。 LoadContent produces a dependency object that represents the tree of content for a given datum. LoadContent生成一个依赖项对象,该对象表示给定数据的内容树。 From what I have found elsewhere, the common use for LoadContent might look like the following: 根据我在其他地方发现的内容,LoadContent的常见用法可能如下所示:

foreach (object point in PointsSource)
        {
            FrameworkElement pointElement = _PointsTemplate.LoadContent() as FrameworkElement;
            pointElement.DataContext = point;
            this.Children.Add(pointElement);
        }

The above code will add a content tree for every single element of data, and we give it the datum to bind its DataContext to. 上面的代码将为每个单独的数据元素添加一个内容树,并为它提供将其DataContext绑定到的数据。

If anyone has working knowledge of creating a custom data control with it's own DataTemplate properties for changing the UI tree for each data element, I would really appreciate it. 如果任何人都有使用它自己的DataTemplate属性创建自定义数据控件的工作知识来更改每个数据元素的UI树,我真的很感激。

Basically, you will want to use an ItemsControl inside your control template, and bind its ItemsSource and ItemTemplate properties to your custom Dependency Properties. 基本上,您需要在控件模板中使用ItemsControl ,并将其ItemsSourceItemTemplate属性绑定到自定义依赖项属性。 Ie, 也就是说,

<Style TargetType="{x:Type local:CustomControl}">
    <Setter Property="Template">
        <ControlTemplate TargetType="{x:Type local:CustomControl}">
            <ItemsControl ItemsSource="{TemplateBinding PointsSource}"
                          ItemTemplate="{TemplateBinding PointsTemplate}"
            />
        </ControlTemplate>
    </Setter>
</Style>

(assuming the DPs IEnumerable - "PointsSource" and DataTemplate - "PointsTemplate") (假设DPs IEnumerable - “PointsSource”和DataTemplate - “PointsTemplate”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM