链接两个listview的行的高度

时间:2013-02-08 01:10:56

标签: wpf

我有一个自定义控件,它有两个使用GridViewRowPresenters的listView。我需要两个listView的行高相同。问题是,直到运行时我才知道高度是多少。

也就是说,两者的高度都设置为Auto,当控件呈现时,一个listView的行在ActualHeight = 30,另一个在ActualHeight = 40.我希望它们都是40。

1 个答案:

答案 0 :(得分:0)

如果ListView使用共享项模板,您只需将GridViewRowPresenter包装在网格中,然后在该网格的唯一行上使用SharedSizeGroup。这是如何实现的。

<强> XAML

<UserControl x:Class="WpfApplication1.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>
        <GridViewColumnCollection x:Key="GridViewColumns">
            <GridViewColumn Width="100"
                            DisplayMemberBinding="{Binding Path=Text}"
                            Header="Text" />
        </GridViewColumnCollection>
        <DataTemplate x:Key="RowTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" SharedSizeGroup="RowSizeGroup" />
                </Grid.RowDefinitions>
                <Border BorderBrush="Black" BorderThickness="1">
                    <GridViewRowPresenter Columns="{StaticResource GridViewColumns}" />
                </Border>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid IsSharedSizeScope="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <GridViewHeaderRowPresenter Columns="{StaticResource GridViewColumns}" />
        <ListView Grid.Row="1"
                  ItemTemplate="{StaticResource RowTemplate}"
                  ItemsSource="{Binding Path=Items1}" />
        <ListView Grid.Row="2"
                  ItemTemplate="{StaticResource RowTemplate}"
                  ItemsSource="{Binding Path=Items2}" />
    </Grid>
</UserControl>

代码背后(供参考,但不是很重要)

using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UserControl2
    {
        public UserControl2()
        {
            InitializeComponent();
            DataContext = this;
            Items1 = new ObservableCollection<object>
                {
                    new Item {Text = "Hello World!"},
                    new Item {Text = "Hello \nWorld!"}
                };
            Items2 = new ObservableCollection<object>
                {
                    new Item {Text = "Testing 1\n2\n3"}
                };
        }

        private class Item
        {
            public string Text { get; set; }
        }

        public ObservableCollection<object> Items1 { get; private set; }

        public ObservableCollection<object> Items2 { get; private set; }
    }
}

如果您想明确设置行的高度,只需将<RowDefinition Height="Auto" SharedSizeGroup="RowSizeGroup" />替换为<RowDefinition Height="40" SharedSizeGroup="RowSizeGroup" />,其中40就是特定的高度。