【问题标题】:Align items in ItemsControl horizontally (no xaml)水平对齐 ItemsControl 中的项目(无 xaml)
【发布时间】:2017-06-01 11:42:14
【问题描述】:

我正在寻找一种方法来设置 ItemsControl 中项目的对齐方式,以便它们不是从上到下(默认)而是从左到右定位。

我认为有一个可以轻松设置的属性,例如

ItemsControl crtl = new ItemsControl;
...
ctrl.ContentAlignment = ContentAlignment.Horizontal; // does not exist

但似乎情况并非如此。 XAML 中有无数示例可以实现这一点(通常它们使用样式或模板或类似的东西),但我在纯 C# 代码中没有找到。而且我也不知道如何将 xaml 语句转换为 C# 代码。

谁能给个提示?

【问题讨论】:

标签: c# wpf itemscontrol


【解决方案1】:

让我们尝试在这里使用属性 O​​rientation:ItemsControl with horizontal orientation‌​‌​h-horizo​​ntal-orien‌​ta‌​tion

这是一个例子:

 var itemsControl = new ItemsControl();    
 var factoryPanel = new FrameworkElementFactory(typeof(StackPanel));
 factoryPanel.SetValue(Panel.IsItemsHostProperty, true);

 factoryPanel.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);    
 var template = new ItemsPanelTemplate {VisualTree = factoryPanel};       
 itemsControl.ItemsPanel = template;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    相关资源
    最近更新 更多