【发布时间】:2011-01-24 22:51:16
【问题描述】:
我正在尝试为列表框动态创建数据模板。这是一个自定义用户控件。这个 UserControl 有一个 DependencyProperty,它接受任何类型的 IEnumerable。
这很好用......但输出总是
- 属性/价值
- 属性/价值
如果对象包含 2 个属性。但我希望这些属性并排排列。喜欢:
对象 1:
- 财产/价值财产/价值
对象 2:
- 属性/值 属性/值
那么我哪里错了?我首先制作一个 Stackpanel,然后在 Stackpanel 中制作包含标签的 Dockpanels。
这里有一个小预览它现在的样子。
这是我创建数据模板的代码:
DataTemplate _NewData = new DataTemplate();
FrameworkElementFactory _template = new FrameworkElementFactory(typeof(StackPanel));
foreach (var _FProperty in view.CurrentItem.GetType().GetProperties())
{
FrameworkElementFactory _firstTemplateChild = new FrameworkElementFactory(typeof(DockPanel));
FrameworkElementFactory _childOneForDock = new FrameworkElementFactory(typeof(Label));
_childOneForDock.SetValue(Label.ContentProperty, _FProperty.Name);
_firstTemplateChild.AppendChild(_childOneForDock);
FrameworkElementFactory _childTwoForChild = new FrameworkElementFactory(typeof(Label));
_childTwoForChild.SetBinding(Label.ContentProperty, new Binding(_FProperty.Name));
_firstTemplateChild.AppendChild(_childTwoForChild);
_template.AppendChild(_firstTemplateChild);
}
_NewData.VisualTree = _template;
ListBoxInPopUp.ItemTemplate = _NewData;
【问题讨论】:
标签: c# wpf custom-controls datatemplate frameworkelement