【问题标题】:UserControl inside horizontal ListView cannot fill parent vertically水平 ListView 内的 UserControl 无法垂直填充父级
【发布时间】: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


【解决方案1】:

我无法找到当前问题的解决方案。但就我想要的行为而言,我通过这种方式实现了这一点: 我从布局中删除了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 方法。由于这不能回答问题,我不会接受它作为答案,希望得到可靠的答案。

【讨论】:

    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多