我需要水平显示UserControl
中每个项目的ListView
。可以使用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
,但事实并非如此。
答案 0 :(得分:0)
我无法找到当前问题的解决方案。但就我想要的行为而言,我通过这种方式实现了这一目标:
我从布局中删除了ListView
。添加了空Grid
。在后台代码中,我添加了这个
public MainWindow()
{
var model=new MainViewModel(loadGrid);
DataContext = model;
InitializeComponent();
Closing += (s, e) => ViewModelLocator.Cleanup();
}
void loadGrid(IList<LanguagePanelViewModel> panels)
{
if (panels == null || !panels.Any())
return;
for (int i = 0; i < panels.Count; i++)
{
mainGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star)});
var panel = new LanguagePanel { DataContext = panels[i] };
Grid.SetColumn(panel, i);
mainGrid.Children.Add(panel);
}
}
我试图不破坏MVVM规则,当我的相关列表发生变化时,会调用这个loadGrid
方法。由于这不能回答这个问题,我不会接受它作为答案,希望得到健全的答案。