.net 在 WPF 中拉伸控件以填充 ListView 列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/493345/
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
In WPF Stretch a control to fill a ListView Column
提问by Ray
How do I make a control fill the whole cell in a ListView (with a GridView)? I've played around with various properties, but the control is always it's minimum size.
如何让控件填充 ListView 中的整个单元格(使用 GridView)?我玩过各种属性,但控件始终是最小尺寸。
Here's my xaml.
这是我的 xaml。
<Window x:Class="ListViewUserControlTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewUserControlTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="UserControlCell">
<Rectangle Fill="Red" HorizontalAlignment="Stretch" MinWidth="10" MinHeight="10" />
</DataTemplate>
</Window.Resources>
<Grid>
<ListView Name="MyListView">
<ListView.View>
<GridView>
<GridViewColumn Width="30" Header="Col1" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="200" Header="Col2" CellTemplate="{StaticResource UserControlCell}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
EditSorry I changed the question because I had eliminated the user control as the source of the problem. It happens with any control.
编辑对不起,我改变了问题,因为我已经消除了用户控制作为问题的根源。它发生在任何控制下。
回答by Ray
After narrowing down my question I was about to Google and find an answer here.
Basically for some reason the ListViewItems are set to align to the Left. Setting them to Stretch fixes this problem. This is done through a style like so:
基本上出于某种原因,ListViewItems 被设置为向左对齐。将它们设置为 Stretch 可以解决此问题。这是通过这样的样式完成的:
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
This unfortunately affects every column, but then you can set the contents of other columns to left, right, center as explained in the link.
不幸的是,这会影响每一列,但是您可以按照链接中的说明将其他列的内容设置为左、右、中。
回答by Peter pete
Another almost-solution is to do something as follows in your datatemplate that goes into the gridviewcolumn
另一个几乎解决方案是在进入 gridviewcolumn 的数据模板中执行以下操作
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor , AncestorType=ListViewItem, AncestorLevel=1},Path=ActualWidth}">
This results in it stretching the textbox to the width of the column, but the right edge is clipped and not rendered. I haven't fixed that part yet. So the already accepted answer is still probably the best one.
这导致它将文本框拉伸到列的宽度,但右边缘被剪裁而不是呈现。我还没有修复那部分。所以已经接受的答案仍然可能是最好的答案。
Here's a sample DataTemplate which does this.
这是执行此操作的示例 DataTemplate。
<DataTemplate DataType="{x:Type local:EditableStringElement}"
x:Key="cellSalutation">
<TextBox Text="{Binding _RecipientData._Salutation.Value , Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsEnabled="{Binding _RecipientData._Salutation.IsEnabled}"
Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor , AncestorType=ListViewItem, AncestorLevel=1},Path=ActualWidth}">
</TextBox>
回答by mjfgates
Your user control specifies the values for the "Height" and "Width" properties. Nothing is allowed to override properties you have explicitly set. Remove "Height" and "Width" and the control's container will be able to mess with the size.
您的用户控件指定“高度”和“宽度”属性的值。不允许任何内容覆盖您明确设置的属性。删除“高度”和“宽度”,控件的容器将能够弄乱大小。
You might want to set a reasonable "MinHeight" and "MinWidth" to prevent containers from squishing you.
您可能希望设置合理的“MinHeight”和“MinWidth”以防止容器挤压您。
Different containers do different things to your control's size; for example, Grid expands you to fill the grid cell you're in, while StackPanel shrinks you to your minimum size. So you might need to deal with that too.
不同的容器对控件的大小做不同的事情;例如,Grid 扩展您以填充您所在的网格单元,而 StackPanel 将您缩小到您的最小尺寸。所以你可能也需要处理这个问题。

