C#WPF在Stackpanel中以编程方式创建的DataTemplate Dockpanel无效 [英] C# WPF Programatically created DataTemplate Dockpanel in Stackpanel has no effect

查看:162
本文介绍了C#WPF在Stackpanel中以编程方式创建的DataTemplate Dockpanel无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为列表框动态创建一个数据模板。这是用于自定义UserControl的。此UserControl具有 DependencyProperty ,它可以接受任何类型的 IEnumerable<>



这很好...但是输出始终是




  • 属性/价值

  • 属性/价值



如果对象包含2个属性。但我希望属性并排排列。像:



对象1:




  • 财产/价值财产/价值



对象2:




  • 财产/价值属性/值



那么我在哪里错了?我首先要制作一个Stackpanel,然后在Stackpanel中制作包含标签的Dockpanels。



以下是当前情况的预览。



这是我创建数据模板的代码:

  DataTemplate _NewData = new DataTemplate(); 
FrameworkElementFactory _template = new FrameworkElementFactory(typeof(StackPanel));

foreach(view.CurrentItem.GetType()。GetProperties()中的_FProperty)
{
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 它指出:

 <!-此StackPanel下的项目垂直堆叠。请注意,方向
的默认值为 Vertical,但在此示例中,为清楚起见,明确设置了属性
。 ->

(拼写错误都是他们的)



DockPanel.Dock 属性的默认值是Left,尽管我不确定在这种情况下这是否是一个因素。

来源


I'm trying to dynamically create a datatemplate for a listbox. This is for a Custom UserControl. This UserControl has a DependencyProperty which accepts any types of IEnumerable<>.

This works fine... But the Output is always

  • Property / Value
  • Property / Value

if the objects contains 2 Properties. But i want that the properties are arranged side by side. Like:

Object 1:

  • Porperty / Value Property / Value

Object 2:

  • Property / Value Property / Value

So where am i wrong ? i'm making first a Stackpanel and in the Stackpanel, Dockpanels which contain the Labels.

Here is a little preview how it looks at the moment.

So this is my code to create the 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;

解决方案

The default orientation of a StackPanel is vertical.

You'll need to set the orientation to horizontal to get the contents to appear side-by-side.

In the code example in the MSDN it states:

<!-- 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. -->

(spelling mistakes are all theirs)

Also the default value of the DockPanel.Dock property is Left, though I'm not sure if that's a factor in this case.

Source

这篇关于C#WPF在Stackpanel中以编程方式创建的DataTemplate Dockpanel无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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