简体   繁体   English

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

[英]How to “Fill” a GridViewColumn with ComboBox (WPF)?

I am looking for a way to "completely fill" a GridViewColumn with a combo box. 我正在寻找一种用组合框“完全填充” GridViewColumn的方法。 I am able to create a cell template with ComboBox and it is working fine. 我能够使用ComboBox创建一个单元格模板,它工作正常。 But the width and height of ComboBox is not aligned with the GridViewColumn . ComboBox的宽度和高度未与GridViewColumn对齐。 Even if I try to set the same height/width GridViewColumn hides some part of the comboBox. 即使我尝试设置相同的高度/宽度, GridViewColumn隐藏了comboBox的某些部分。

There must be some setting or style to instruct WPF to fill the ComboBox completely in the available space of GridViewColumn 必须有一些设置或样式来指示WPF在GridViewColumn的可用空间中完全填充ComboBox

This is my XAML. 这是我的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>

Include the following style into the ListViews-resources. 将以下样式包含在ListViews资源中。 Then you can set the HorizontalAlignment-property of the ComboBox to HorizontalAlignment="Stretch" and it will do as you wish: 然后你可以将ComboBox的Horizo​​ntalAlignment属性设置为HorizontalAlignment="Stretch" ,它将按你的意愿执行:

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

Have you tried this: 你试过这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM