2

リストボックスのデータ テンプレートを動的に作成しようとしています。これは、カスタム UserControl 用です。この UserControl には、任意のタイプのIEnumerable<>を受け入れるDependencyPropertyがあります。

これは正常に動作します...しかし、出力は常に

  • プロパティ/値
  • プロパティ/値

オブジェクトに 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;
4

1 に答える 1

2

a のデフォルトの方向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は Left ですが、それがこの場合の要因かどうかはわかりません。

ソース

于 2011-01-24T22:57:13.980 に答える