wpf 在 ListView ItemTemplate 填充中制作网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16832725/
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
Making a grid in a ListView ItemTemplate fill
提问by havardhu
I have a ListView which has a data template based on a grid. The xaml is as follows:
我有一个 ListView,它有一个基于网格的数据模板。xaml 如下:
<ListView ItemsSource="{Binding SomeItemSource}" HorizontalAlignment="Stretch" Height="281">
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch" Margin="3" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<!-- Grid content here -->
<TextBlock Text="SomeTextbox"
Margin="5,5,0,0"/>
<TextBox Grid.Column="1"
Margin="0,5,0,0"
Text="{Binding SomeProperty, Mode=TwoWay}"
HorizontalAlignment="Left"
Width="90"/>
<TextBlock Text="AnotherText"
Grid.Column="0"
Grid.Row="1"
Margin="5,5,0,0"/>
<TextBox Grid.Column="1" Grid.Row="1"
Margin="0,5,0,0"
Text="{Binding AnotherProperty, Mode=TwoWay}"
HorizontalAlignment="Stretch"
Width="300"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
What I want, is for the grid to fill (stretch over) the entire horizontal width of the parent ListView, however it currently wraps to the width of the sum of its contents. How can I achieve the behavior I want?
我想要的是让网格填充(拉伸)父 ListView 的整个水平宽度,但是它当前包装到其内容总和的宽度。我怎样才能实现我想要的行为?
回答by dkozl
You need to set ListViewItem.HorizontalContentAlignmentto Stretch. Try adding this into your ListViewdefinition:
您需要设置ListViewItem.HorizontalContentAlignment为Stretch. 尝试将其添加到您的ListView定义中:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.ItemContainerStyle>
回答by A.Kosecik
<ListView Name="Husam_Copy" HorizontalAlignment="Left" Height="181" Margin="60,86,0,0" VerticalAlignment="Top" Width="189" >
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Title}" HorizontalAlignment="Stretch" Padding="0" Margin="0" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

