在WPF GridView的动态列宽 [英] GridView dynamic column width in WPF

查看:545
本文介绍了在WPF GridView的动态列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Window x:Class="GridViewSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" >
<Grid>
	<Grid.RowDefinitions>
		<RowDefinition/>
		<RowDefinition/>
	</Grid.RowDefinitions>
	<ListView Name="CheckoutList" Margin="0,0,8,0">
		<ListView.View>
			<GridView x:Name="CheckOutGridView">
				<GridView.ColumnHeaderContainerStyle>
					<Style TargetType="{x:Type GridViewColumnHeader}">
						<Setter Property="Visibility" Value="Collapsed"/>
					</Style>
				</GridView.ColumnHeaderContainerStyle>
				<GridViewColumn>
					<GridViewColumn.CellTemplate>
						<DataTemplate>
							<TextBlock> 
								<Hyperlink >          
									<TextBlock Text="{Binding Path=Text}"/> 
								</Hyperlink> 
							</TextBlock>
						</DataTemplate>
					</GridViewColumn.CellTemplate>
				</GridViewColumn>
				<GridViewColumn>
					<GridViewColumn.CellTemplate>
						<DataTemplate>
							<TextBlock> 
								<Hyperlink>          
									<TextBlock Text="Remove"/> 
								</Hyperlink> 
							</TextBlock>
						</DataTemplate>
					</GridViewColumn.CellTemplate>
				</GridViewColumn>
			</GridView>
		</ListView.View>
	</ListView>

	<StackPanel Grid.Row="1">
		<Button Width="100" Height="25" Click="Button_Click" >name</Button>
		<Button Width="100" Height="25" Click="Button_Click1" >long name</Button>
	</StackPanel>
</Grid>

public partial class Window1 : Window
{
	class TextObject
	{
		private string _text;

		public TextObject(string Text)
		{
			_text = Text;
		}

		public string Text
		{
			get { return _text; }
			set { _text = value; }
		}
	}


	public Window1()
	{
		InitializeComponent();
	}

	private void Button_Click(object sender, RoutedEventArgs e)
	{
		CheckoutList.Items.Add(new TextObject("name"));
	}

	private void Button_Click1(object sender, RoutedEventArgs e)
	{
		CheckoutList.Items.Add(new TextObject("long name"));
	}

}

鉴于上述样品preSS名再长的名字。为什么列不调整大小以适应其内容?

Given the above sample press name then long name. How come the column doesn't resize to fit its contents?

推荐答案

尝试

<GridViewColumn Width="{x:Static System:Double.NaN}">

其中,

xmlns:System="clr-namespace:System;assembly=mscorlib"

这应该可以帮助您调整列以适应头部的内容。

and this should help you resize the column to fit the header content.

这篇关于在WPF GridView的动态列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆