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