如何使用ComboBox(WPF)“填充”GridViewColumn?

时间:2010-08-19 12:58:09

标签: c# wpf listview gridview combobox

我正在寻找一种用组合框“完全填充”GridViewColumn的方法。我能够使用ComboBox创建一个单元格模板,它工作正常。但ComboBox的宽度和高度未与GridViewColumn对齐。 即使我尝试设置相同的高度/宽度GridViewColumn也隐藏了comboBox的某些部分。

必须有一些设置或样式来指示WPF在ComboBox的可用空间中完全填充GridViewColumn

这是我的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>

2 个答案:

答案 0 :(得分:10)

将以下样式包含在ListViews资源中。然后,您可以将ComboBox的Horizo​​ntalAlignment属性设置为HorizontalAlignment="Stretch",它将按您的意愿执行:

 <ListView.Resources>
          <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          </Style>
 </ListView.Resources>

答案 1 :(得分:1)

你试过这个:

<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" />