如何自动调整大小和WPF右对齐GridViewColumn数据? [英] How to autosize and right-align GridViewColumn data in WPF?
问题描述
如何
- 右键对齐在ID列中的文本
- 根据小区的文本长度最长可见数据使每一列汽车大小?
下面是code:
< ListView控件名称=lstCustomers的ItemsSource ={绑定路径=集合}>
< ListView.View>
<&GridView的GT;
< GridViewColumn头=IDDisplayMemberBinding ={绑定ID}WIDTH =40/>
< GridViewColumn标题=名DisplayMemberBinding ={结合姓}WIDTH =100/>
< GridViewColumn标题=姓DisplayMemberBinding ={结合姓氏}/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
部分答案:
感谢的Kjetil的GridViewColumn.CellTemplate运作良好,并自动宽度工作当然但当ObservativeCollection集与时间长于列宽的数据更新,所以这是一个仅列大小不更新自己数据的初始显示的解决方案:
< ListView控件名称=lstCustomers的ItemsSource ={绑定路径=集合}>
< ListView.View>
<&GridView的GT;
< GridViewColumn头=IDWIDTH =自动>
< GridViewColumn.CellTemplate>
<&DataTemplate的GT;
< TextBlock的文本={绑定ID}TextAlignment =右WIDTH =40/>
< / DataTemplate中>
< /GridViewColumn.CellTemplate>
< / GridViewColumn>
< GridViewColumn标题=名DisplayMemberBinding ={结合姓}WIDTH =自动/>
< GridViewColumn标题=姓DisplayMemberBinding ={结合姓氏}WIDTH =自动/>
< / GridView的>
< /ListView.View>
< /&的ListView GT;
为使每一列AUTOSIZE您可以在GridViewColumn设置宽度=自动。
要右对齐的ID列中的文本,您可以创建一个使用一个TextBlock细胞模板,设置TextAlignment。然后设置ListViewItem.HorizontalContentAlignment(使用上ListViewItem的一个setter风格),以使细胞模板填充整个GridViewCell。
也许还有一个更简单的解决方案,但这应该工作。
注意:解决方案需要双方的 HorizontalContentAlignment =拉伸</ STRONG>在Window.Resources和 TextAlignment =右在CellTemplate
&LT;窗口x:类=WpfApplication6.Window1
的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
的xmlns:X =http://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300&GT;
&LT; Window.Resources&GT;
&LT;风格的TargetType =ListViewItem的&GT;
&LT; setter属性=HorizontalContentAlignmentVALUE =弹力/&GT;
&LT; /样式和GT;
&LT; /Window.Resources>
&LT;网格和GT;
&LT; ListView控件名称=lstCustomers的ItemsSource ={绑定路径=集合}&GT;
&LT; ListView.View&GT;
&LT;&GridView的GT;
&LT; GridViewColumn头=IDWIDTH =40&GT;
&LT; GridViewColumn.CellTemplate&GT;
&LT;&DataTemplate的GT;
&LT; TextBlock的文本={绑定ID}TextAlignment =右/&GT;
&LT; / DataTemplate中&GT;
&LT; /GridViewColumn.CellTemplate>
&LT; / GridViewColumn&GT;
&LT; GridViewColumn标题=名DisplayMemberBinding ={结合姓}WIDTH =自动/&GT;
&LT; GridViewColumn标题=姓DisplayMemberBinding ={结合姓氏}WIDTH =自动/&GT;
&LT; / GridView的&GT;
&LT; /ListView.View>
&LT; /&的ListView GT;
&LT; /网格和GT;
&LT; /窗GT;
How can I:
- right-align the text in the ID column
- make each of the columns auto size according to the text length of the cell with the longest visible data?
Here is the code:
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" DisplayMemberBinding="{Binding Id}" Width="40"/>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="100" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
</GridView>
</ListView.View>
</ListView>
partial answer:
Thanks Kjetil, the GridViewColumn.CellTemplate works well and the Auto Width works of course but when the ObservativeCollection "Collection" is updated with longer-than-column-width data, the column sizes do not update themselves so that is only a solution for the initial display of data:
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" TextAlignment="Right" Width="40"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
To make each of the columns autosize you can set Width="Auto" on the GridViewColumn.
To right-align the text in the ID column you can create a cell template using a TextBlock and set the TextAlignment. Then set the ListViewItem.HorizontalContentAlignment (using a style with a setter on the ListViewItem) to make the cell template fill the entire GridViewCell.
Maybe there is a simpler solution, but this should work.
Note: the solution requires both HorizontalContentAlignment=Stretch in Window.Resources and TextAlignment=Right in the CellTemplate.
<Window x:Class="WpfApplication6.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</Window.Resources>
<Grid>
<ListView Name="lstCustomers" ItemsSource="{Binding Path=Collection}">
<ListView.View>
<GridView>
<GridViewColumn Header="ID" Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Id}" TextAlignment="Right" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding FirstName}" Width="Auto" />
<GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding LastName}" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
这篇关于如何自动调整大小和WPF右对齐GridViewColumn数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!