WPF:扩展 ListView 的 GridView 的最后一列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/911243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF : Extend last column of ListView's GridView
提问by THX-1138
I have a ListView with a GridView with 3 columns. I want last column to take up remaining width of the ListView.
我有一个带有 3 列的 GridView 的 ListView。我希望最后一列占据 ListView 的剩余宽度。
采纳答案by gcores
That can't be done with simple XAML, but there are some solutions out there. Check this out:
使用简单的 XAML 无法做到这一点,但有一些解决方案。看一下这个:
回答by pr0gg3r
quick & dirty
又快又脏
xaml:
xml:
<ListView SizeChanged="ListView_SizeChanged" Loaded="ListView_Loaded" >
<ListView.View>
<GridView>
<GridViewColumn Header="col1" Width="100" />
<GridViewColumn Header="col1" Width="Auto" />
<GridViewColumn Header="col1" />
</GridView>
</ListView.View>
</ListView>
cs:
CS:
private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateColumnsWidth(sender as ListView);
}
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
UpdateColumnsWidth(sender as ListView);
}
private void UpdateColumnsWidth(ListView listView)
{
int autoFillColumnIndex = (listView.View as GridView).Columns.Count - 1;
if (listView.ActualWidth == Double.NaN)
listView.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
double remainingSpace = listView.ActualWidth;
for (int i = 0; i < (listView.View as GridView).Columns.Count; i++)
if (i != autoFillColumnIndex)
remainingSpace -= (listView.View as GridView).Columns[i].ActualWidth;
(listView.View as GridView).Columns[autoFillColumnIndex].Width = remainingSpace >= 0 ? remainingSpace : 0;
}
回答by Rolf Wessels
There is a way to do it using behavior pattern
有一种方法可以使用行为模式来做到这一点
<ListView HorizontalAlignment="Stretch"
? ? ? ? ? Behaviours:GridViewColumnResize.Enabled="True">
? ? ? ? <ListViewItem></ListViewItem>
? ? ? ? <ListView.View>
? ? ? ? ? ? <GridView>
? ? ? ? ? ? ? ? <GridViewColumn ?Header="Column *"
? ? ?? ? ?? ? ??? ?? ? ? ? ? ? ? ? Behaviours:GridViewColumnResize.Width="*" >
? ? ? ? ? ? ? ? ? ? <GridViewColumn.CellTemplate>
? ? ? ? ? ? ? ? ? ? ? ? <DataTemplate>
? ? ? ? ? ? ? ? ? ? ? ? ? ? <TextBox HorizontalAlignment="Stretch" Text="Example1" />
? ? ? ? ? ? ? ? ? ? ? ? </DataTemplate>
? ? ? ? ? ? ? ? ? ? </GridViewColumn.CellTemplate>
See the following link for some examples and link to read more http://lazycowprojects.tumblr.com/post/7063214400/wpf-c-listview-column-width-auto
请参阅以下链接以获取一些示例和链接以阅读更多内容http://lazycowprojects.tumblr.com/post/7063214400/wpf-c-listview-column-width-auto
And to see the source code. Check out https://github.com/rolfwessels/lazycowprojects/tree/master/Wpf
并查看源代码。查看 https://github.com/rolfwessels/lazycowprojects/tree/master/Wpf
回答by Pale Ale
How About Using a Style
如何使用样式
<Style x:Key="GridViewExtraStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
<Setter Property="Width" Value="1000"/>
</Style>
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Abc"/>
<GridViewColumn Header="" HeaderContainerStyle="{DynamicResource GridViewExtraStyle}"/>
</GridView>
</ListView.View>
</ListView>
回答by Pale Ale
I haven't seen a one-line/simple XAML only solution. Set a width that's appropriate for the design view, and then modify the width on window size change like this:
我还没有看到单行/简单的 XAML 解决方案。设置适合设计视图的宽度,然后像这样修改窗口大小更改的宽度:
Private Sub winMain_SizeChanged(sender As Object, e As SizeChangedEventArgs) Handles Me.SizeChanged
TryCast(lvwDownload.View, GridView).Columns(3).Width = lvwDownload.ActualWidth - 340
End Sub
NOTE: This logic does not change/hover the width of a column when another is resized. It's great for filling a listview with the last column.
注意:当调整另一列的大小时,此逻辑不会更改/悬停列的宽度。用最后一列填充列表视图非常有用。
回答by user2334141
I used Pale Ales's suggestion with a minor change:
我使用了 Pale Ales 的建议,并稍作改动:
<Style x:Key="GridViewExtraStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="{x:Null}"/>
<Setter Property="BorderBrush" Value="{x:Null}"/>
</Style>
<ListView>
<ListView.View>
<GridView ColumnHeaderContainerStyle="{DynamicResource GridViewExtraStyle}">
<GridViewColumn Header="Abc" Width="{Binding Path=mywidth}"/>
</GridView>
</ListView.View>
</ListView>
回答by maximgorki
You cannot be remove last column but you can be doin little illusion.
你不能删除最后一列,但你可以做一点错觉。
<ControlTemplate TargetType="GridViewColumnHeader">
<Grid>
<ContentPresenter x:Name="HeaderContent"
Content="{TemplateBinding Content}" ... />
<Thumb x:Name="PART_HeaderGripper" ... />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasContent"
Value="false">
<Setter Property="Visibility"
Value="Collapsed"
TargetName="PART_HeaderGripper" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>