C # WPF Programatically creato DataTemplate DockPanel in StackPanel non ha alcun effetto
-
24-10-2019 - |
Domanda
Sto cercando di creare dinamicamente un DataTemplate per una casella di riepilogo. Questo è per un UserControl personalizzato. Questo UserControl ha un DependencyProperty che accetta tutti i tipi di IEnumerable <> .
Questo funziona bene ... ma l'uscita è sempre
- Proprietà / Valore
- Proprietà / Valore
se gli oggetti contiene 2 Proprietà. Ma voglio che le proprietà sono disposti fianco a fianco. Come:
Oggetto 1:
- porperty / Valore proprietà / Valore
Oggetto 2:
- Proprietà / Property Value / Valore
Quindi, dove sbaglio? Sto facendo prima uno StackPanel e nel StackPanel, Dockpanels che contengono le etichette.
Ecco una piccola anteprima come appare in questo momento.
Quindi questo è il mio codice per creare il 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;
Soluzione
L'orientamento predefinito di un StackPanel
è verticale.
Sarà necessario impostare l'orientamento a orizzontale per ottenere il contenuto di apparire fianco a fianco.
Nel esempio di codice nella MSDN afferma:
<!-- 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. -->
(errori di ortografia sono tutti loro)
Anche il valore predefinito della proprietà DockPanel.Dock
è a sinistra, anche se non sono sicuro se questo è un fattore in questo caso.