如何使用其自己的DataTemplate DependencyProperty实现WPF控件? [英] How do I implement a WPF control with its own DataTemplate DependencyProperty?

查看:45
本文介绍了如何使用其自己的DataTemplate DependencyProperty实现WPF控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WPF的中级开发人员,具有有关如何实现依赖项属性以及简单的自定义控件的工作知识.我还不了解如何将DataTemplate依赖项属性添加到自定义控件,并使用它为数据集中的每个数据定义元素树.

整个故事是,我一直在努力创建WPF地图控件,该控件在地图图块上的地图上显示许多不同的点和几何形状.当用户拖动"周围的地图时,这些形状将与地图的其余部分一起转换.

在创建地图控件的范围内,我已经完成了此任务,并且可以在具有地图坐标的Xaml中向其添加子元素.我想更进一步,并添加数据集合的属性,例如点,区域等.为了更好地理解我要查找的内容,我想从ListBox重新创建两个属性:ItemsSource和ItemTemplate./p>

我向我的地图控件添加了两个依赖项属性-PointsSource和PointsTemplate.PointsSource类型为IEnumberable,表示要在地图上显示的数据集合.PointsTemplate代表每个基准面的外观.仅将这些属性放到我的控件中显然是不够的,但是我不确定如何相互协调.如果有人了解如何使用其自己的DataTemplate属性创建自定义数据控件来更改每个数据元素的UI树,那么我将不胜感激.

解决方案

我已经在DataTemplate本身中找到了想要的东西.DataTemplate为后面的代码提供了一个称为LoadContent()的函数.LoadContent生成一个依赖对象,该对象表示给定数据的内容树.根据我在其他地方发现的内容,LoadContent的常见用法如下所示:

  foreach(PointsSource中的对象点){FrameworkElement pointElement = _PointsTemplate.LoadContent()作为FrameworkElement;pointElement.DataContext =点;this.Children.Add(pointElement);} 

上面的代码将为数据的每个单个元素添加一个内容树,并为它提供数据绑定到其DataContext的数据.

I am an intermediate WPF developer, with working knowledge on how to implement dependency properties as well as simple custom controls. 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.

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. 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. I would like to take this farther, and add properties for collections of data, i.e. points, areas, etc. To better understand what I'm looking for, I would like to re-create two properties from ListBox: ItemsSource and ItemTemplate.

I have added two dependency properties to my Map control - PointsSource and PointsTemplate. PointsSource is of type IEnumberable and represents the collection of data to display on the map. PointsTemplate represents what each of those datum should look like. 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.

解决方案

I have found what I am looking for in the DataTemplate itself. The DataTemplate provides a function for code behind called LoadContent(). LoadContent produces a dependency object that represents the tree of content for a given datum. From what I have found elsewhere, the common use for LoadContent might look like the following:

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.

这篇关于如何使用其自己的DataTemplate DependencyProperty实现WPF控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆