One way is to define a new control template, but all ItemsControls provide a shortcut with its ItemsPanel property. ItemsPanel enables you to swap out the panel used to arrange items while leaving everything else about the control intact. ListBox uses a panel called VirtualizingStackPanel to arrange its items vertically, but the following code replaces it with a new VirtualizingStackPanel that explicitly sets its Orientation to Horizontal:
The translation of this XAML to procedural code is not straightforward, but here’s how you
can accomplish the same task in C#:
FrameworkElementFactory panelFactory =
new FrameworkElementFactory(typeof(VirtualizingStackPanel));
panelFactory.SetValue(VirtualizingStackPanel.OrientationProperty,
Orientation.Horizontal);
myListBox.ItemsPanel = new ItemsPanelTemplate(panelFactory);
No comments:
Post a Comment