【发布时间】:2016-11-21 16:43:38
【问题描述】:
我需要为ListView 中的每个项目水平显示UserControl。可以使用StackPanel 来完成,但据我所知StackPanel 将换行直到可以放置内容。但我需要垂直填充父项(在此示例中为 ListView),例如其 Grid 与多个 ListBoxes。顺便说一句,我的UserControl是这样的:
<UserControl x:Class="ResourceEditor.LanguagePanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:resources="clr-namespace:ResourceEditor.Resources"
xmlns:viewModel="clr-namespace:ResourceEditor.ViewModel">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="1" LastChildFill="True" DockPanel.Dock="Bottom">
<CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" DockPanel.Dock="Right" IsChecked="{Binding SortByThis}"></CheckBox>
<UniformGrid Columns="2" Rows="0">
<Button Content="{x:Static resources:Strings.Save}" DockPanel.Dock="Left" Command="{Binding SaveCommand}"></Button>
<Button Content="{x:Static resources:Strings.Reset}" DockPanel.Dock="Left" Command="{Binding ResetCommand}"></Button>
</UniformGrid>
</DockPanel>
<ListBox Grid.Row="0" SelectedItem="{Binding SelectedWord}" ItemsSource="{Binding Words}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<command:EventToCommand Command="{Binding Mode=OneTime, Path=EnableEditingCommand}"
PassEventArgsToCommand="False" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<HierarchicalDataTemplate DataType="viewModel:WordNodeVM">
<TextBox IsEnabled="{Binding DataContext.CanEdit, RelativeSource={RelativeSource AncestorType=UserControl}}" Text="{Binding Name}"></TextBox>
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
这就是我使用它的方式:
<ListView Grid.Row="2" ItemsSource="{Binding Languages}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Focusable" Value="false"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" Columns="{Binding Languages.Count}">
</UniformGrid>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<HierarchicalDataTemplate DataType="{ x:Type viewModel:LanguagePanelViewModel}">
<resourceEditor:LanguagePanel DataContext="{Binding}"></resourceEditor:LanguagePanel>
</HierarchicalDataTemplate>
</ListView.ItemTemplate>
</ListView>
我尝试了所有可能的解决方案,更改容器,使用对齐方式,对齐内容,但无法让我的用户控件填充垂直空间。首先我认为UserControl 有问题,因为它的行为可能类似于StackPanel,但也不是。
【问题讨论】:
-
WPF 有一个叫做 WrapPanel 的控件,这就是你要找的吗?
-
我需要垂直填充父级。但是
WrapPanel甚至比StackPanel更糟糕,因为它包含了用户控件的最小尺寸。
标签: c# wpf listview user-controls