我正在寻找一种方法来“完全填充”一个组合框的GridViewColumn。我可以用ComboBox创建一个单元格模板,它工作得很好。但是ComboBox的宽度和高度与GridViewColumn不对齐。即使我尝试设置相同的高度/宽度,GridViewColumn也会隐藏comboBox的某些部分。
必须有某种设置或样式才能指示WPF在GridViewColumn的可用空间中完全填充ComboBox
这是我的XAML。
<Window x:Class="WPFStarter.ComboInsideListView.ComboBoxInsideListViewUsingObject"
x:Name="userControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ComboBoxInsideListViewUsingObject" Height="300" Width="400">
<Grid>
<ListView x:Name="listView" ItemsSource="{Binding ElementName=userControl,
Path=DataContext.Items}" SelectedItem="{Binding ElementName=userControl, Path=DataContext.SelectedItem, Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=First}"/>
<GridViewColumn Header="Gender">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox x:Name="cmb_Gender" Width="75" SelectedValue="{Binding Path=Gender}"
ItemsSource="{Binding ElementName=userControl, Path=DataContext.Genders}" GotFocus="ComboBox_GotFocus" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>发布于 2010-08-19 21:07:18
将以下样式包含到ListViews-resources中。然后,您可以将ComboBox的HorizontalAlignment-属性设置为HorizontalAlignment="Stretch",它将如您所愿:
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.Resources>发布于 2010-08-19 21:03:51
你有没有试过这个:
<ComboBox x:Name="cmb_Gender" Width="75" SelectedValue="{Binding Path=Gender}"
ItemsSource="{Binding ElementName=userControl, Path=DataContext.Genders}"
GotFocus="ComboBox_GotFocus"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />https://stackoverflow.com/questions/3521983
复制相似问题