C#WPF编程创建的DatateMplate Dockpanel在Stackpanel中没有效果
-
24-10-2019 - |
题
我正在尝试动态创建列表框的数据定位板。这是针对自定义USERCORTROL的。这个UserControl有一个 依赖关系 接受任何类型的 ienumerable <>.
这很好...但是输出总是
- 适当的价值
- 适当的价值
如果对象包含2个属性。但是我希望这些属性并排排列。喜欢:
对象1:
- 孢子 /值属性 /值
对象2:
- 财产 /价值属性 /价值
那我在哪里错了?我首先将其制作为Stackpanel,在Stackpanel中,其中包含标签的Dockpanels。
这是目前看起来的一些预览。
因此,这是我创建DataTemplate的代码:
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;
解决方案
默认方向 StackPanel
是垂直的。
您需要将方向设置为水平,以使内容并排出现。
在代码示例中 MSDN 它指出:
<!-- The items under this StackPanel are stacked Vertically. Note that Orientation
has a default value of "Vertical" but in this example the property is explicitely
set for clarity. -->
(拼写错误都是他们的)
也是 DockPanel.Dock
属性剩下,尽管我不确定在这种情况下这是否是一个因素。
不隶属于 StackOverflow