0
我需要为ListView
中的每个项目水平显示UserControl
。它可以用StackPanel
完成,但据我所知StackPanel
将包装,直到内容可以放置。但是我需要填充父项(在此示例中为ListView
),比如它的Grid
与多个ListBoxes
。顺便说一句,我UserControl
是这样的:水平ListView内部的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'更糟糕,因为它包含了用户控制的最小尺寸。 – Javidan