[英]Get height of header of a ListView with GridView
How can I get the height of the header of a ListView
with a GridView
? 如何使用
GridView
获取ListView
标头的高度? Is it even possible? 可能吗?
I need to set the height of one control based on the height of the header of a ListView
so I started writing a Converter
. 我需要根据
ListView
标头的高度设置一个控件的高度,因此我开始编写Converter
。 The problem is that I can't access the actual height. 问题是我无法访问实际高度。
The debugger of Visual Studio shows me that GridView
has a property called HeaderRowPresenter
, which in turn has a property ActualHeight
. Visual Studio的调试器向我显示
GridView
具有一个名为HeaderRowPresenter
的属性,而该属性又具有一个ActualHeight
属性。 But I can't access it, HeaderRowPresenter
seems to be protected or private. 但是我无法访问它,
HeaderRowPresenter
似乎是受保护的或私有的。
All other ColumnHeader*
properties ( ColumnHeaderContainerStyle
, ColumnHeaderTemplate
, etc.) are null
on this object, same for all Header*
properties on the Columns
(except for the String
that is the content of the header). 此对象上的所有其他
ColumnHeader*
属性( ColumnHeaderContainerStyle
, ColumnHeaderTemplate
等)都为null
,这与Columns
上的所有Header*
属性相同(除了String
是标题的内容)。
Btw: I'm trying to solve a different problem and my current approach let me to this, so maybe I'm taking this on the wrong way. 顺便说一句:我正在尝试解决一个不同的问题,而我目前的方法让我可以解决这个问题 ,所以也许我以错误的方式来解决这个问题 。
Okay, I've found a solution but I'm not very proud of it, because it uses reflection: 好的,我找到了一个解决方案,但我对此并不感到骄傲,因为它使用了反射:
I wrote a Converter
that takes a View
and a ListView
as value and parameter and returns the height difference of the headers: 我编写了一个
Converter
,它使用View
和ListView
作为值和参数,并返回标头的高度差:
public class HeightSyncConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
GridView gvLocal = value as GridView;
ListView lvOther = parameter as ListView;
if( gvLocal == null || lvOther == null)
{
return 0;
}
GridView gvOther = (GridView)lvOther.View;
try
{
// get the non-public HeaderRowPresenter property
GridViewHeaderRowPresenter hrpLocal = (GridViewHeaderRowPresenter)gvLocal.GetType().GetProperty("HeaderRowPresenter", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(gvLocal);
GridViewHeaderRowPresenter hrpOther = (GridViewHeaderRowPresenter)gvOther.GetType().GetProperty("HeaderRowPresenter", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(gvOther);
if( hrpLocal == null || hrpOther == null )
{
return 0;
}
// Only works if the other ListView's header is higher than the local one's
if( hrpLocal.ActualHeight > hrpOther.ActualHeight )
{
return 0;
}
return hrpOther.ActualHeight - hrpLocal.ActualHeight;
}
catch(TargetInvocationException) { }
return 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
I then use it to define the height of a Grid.Row
: 然后,我用它来定义
Grid.Row
的高度:
<UserControl x:Class="GUI.ListViewLayout"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GUI"
xmlns:conv="clr-namespace:GUI.Converter"
mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"
Loaded="Window_Loaded">
<UserControl.Resources>
<Style x:Key="verticalGridViewColumnHeader" TargetType="GridViewColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" FontWeight="Bold"
VerticalAlignment="Center" TextAlignment="Center" HorizontalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="270" />
</TextBlock.LayoutTransform>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
<conv:HeightSyncConverter x:Key="myConverter" />
</UserControl.Resources>
<Grid x:Name="grid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150" />
<ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition x:Name="fillerRow" Height="{Binding View, ElementName=listView1, Converter={StaticResource myConverter}, ConverterParameter={x:Reference listView2}}" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button x:Name="someControl" Grid.RowSpan="2">Placeholder</Button>
<ListView x:Name="listView1" Grid.Row="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Header 1" Width="60"/>
<GridViewColumn Header="Header 2" Width="60" />
</GridView>
</ListView.View>
</ListView>
<ListView x:Name="listView2" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2">
<ListView.View>
<GridView>
<GridViewColumn Header="Long Header 1"
HeaderContainerStyle="{StaticResource verticalGridViewColumnHeader}" Width="Auto" />
<GridViewColumn Header="Long Header 2"
HeaderContainerStyle="{StaticResource verticalGridViewColumnHeader}" Width="Auto" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
The problem is that the converter is called before the ListView
s and their headers are initialized, so I reevaluate the Binding
in the code-behind: 问题是转换器在
ListView
和其标头初始化之前被调用,因此我在后面的代码中重新评估Binding
:
public partial class ListViewLayout : UserControl
{
public ListViewLayout()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BindingExpression binding = fillerRow.GetBindingExpression(RowDefinition.HeightProperty);
binding.UpdateTarget();
}
}
All in all, I don't think this is worth the effort and the best way is to define the height of the headers myself instead of retrieving an automatically calculated ActualHeight
. 总而言之,我认为这样做不值得,最好的方法是自己定义标题的高度,而不是检索自动计算的
ActualHeight
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.