C# 具有水平方向的ItemsControl
您知道从ItemsControl继承的任何控件都具有项目的水平方向吗?只需更改用于承载项目的面板:C# 具有水平方向的ItemsControl,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,您知道从ItemsControl继承的任何控件都具有项目的水平方向吗?只需更改用于承载项目的面板: <ItemsControl ...> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </It
<ItemsControl ...>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
最重要的答案是好的,但我无法让它与UserControls一起工作。如果您需要UserControls,这应该会有所帮助 我的版本:
<Window.Resources>
<DataTemplate x:Key="ItemTemplate2">
<StackPanel>
<uc:MyUserControl MinWidth="20" BorderBrush="Black" BorderThickness="0.1" />
</StackPanel>
</DataTemplate>
<ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
<StackPanel Orientation="Horizontal" Margin="0,0,0,0"/>
</ItemsPanelTemplate>
</Window.Resources>
<StackPanel>
<ItemsControl x:Name="list_MyControls"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Margin="0,8,0,0"
ItemTemplate="{StaticResource ItemTemplate2}"
ItemsPanel="{StaticResource ItemsPanelTemplate1}" />
</StackPanel>
要绑定到数据,您需要将
ItemsSource
添加到XAML或代码隐藏中的ItemsControl
。还要注意的是,uc:
将是文件顶部声明的xmlns:uc=“NamespaceOfMyControl”
。虽然升级后的答案很好,但如果您希望项目拉伸,这里有一个替代方案
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
这是一个如何在ItemsControl中进行水平滚动的示例 首先,主窗口viewmodel类用于获取/设置要显示的项目列表 MainWindowViewModel.cs
using System.Collections.Generic;
namespace ItemsControl
{
public class Item
{
public Item(string title)
{
Title = title;
}
public string Title { get; set; }
}
public class MainWindowViewModel
{
public MainWindowViewModel()
{
Titles = new List<Item>()
{
new Item("Slide 1"),
new Item("Slide 2"),
new Item("Slide 3"),
new Item("Slide 4"),
new Item("Slide 5"),
new Item("Slide 6"),
new Item("Slide 7"),
new Item("Slide 8"),
};
}
public List<Item> Titles { get; set; }
}
}
使用System.Collections.Generic;
命名空间项控件
{
公共类项目
{
公共项目(字符串标题)
{
头衔=头衔;
}
公共字符串标题{get;set;}
}
公共类MainWindowViewModel
{
公共主窗口视图模型()
{
标题=新列表()
{
新项目(“幻灯片1”),
新项目(“幻灯片2”),
新项目(“幻灯片3”),
新项目(“幻灯片4”),
新项目(“幻灯片5”),
新项目(“幻灯片6”),
新项目(“幻灯片7”),
新项目(“幻灯片8”),
};
}
公共列表标题{get;set;}
}
}
视图的主窗口为xaml:
main window.xaml
<Window x:Class="ItemsControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ItemsControl"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<Window.DataContext>
<local:MainWindowViewModel />
</Window.DataContext>
<Grid Margin="5">
<ScrollViewer
VerticalScrollBarVisibility="Disabled"
HorizontalScrollBarVisibility="Auto">
<ItemsControl
x:Name="SearchResultList"
ItemsSource="{Binding Titles}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border
Margin="5"
BorderThickness="1"
BorderBrush="Aqua">
<TextBlock
Text="{Binding Title}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontSize="12"
TextWrapping="Wrap"
TextAlignment="Center"
FontWeight="DemiBold"
Width="150"
Height="150" />
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</Window>
根据客户端区域的高度/宽度,这将导致这种布局,溢出项水平滚动:
更多详细信息可在此博客链接中找到,包括如何垂直滚动的示例:
对于包含数百甚至数千项的长列表,您应该更好地使用,以避免性能问题。是否需要将IsItemsHost=“True”添加到StackPanel?我相信只有在模板化整个控件时才有必要这样做。请参阅Silverlight的答案。如何在资源文件中执行此操作?我希望您可以在主标记中执行类似于
StackPanel.Orientation=“Horizontal”
的操作!我不习惯使用WPF,所以也许我要说的是非常基本的东西。我发现在UserControl中应该使用“UserControl.Resources”而不是“Window.Resources”。无论如何,谢谢你的回答,解决了我的问题。如果你使用UWP,你需要UWP UniformGrid从这里开始:。我刚刚实现了它和上面NielW的解决方案。这是一个11年前的问题,我希望它已经解决了;)只是添加了这条评论以使解决方案更加完整:)因为你知道,这个WPF是一个非常不友好的框架,有时不可预测,而且最新的VS2019崩溃非常烦人XD