首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用ComboBox“填充”GridViewColumn?

如何用ComboBox“填充”GridViewColumn?
EN

Stack Overflow用户
提问于 2010-08-19 20:58:09
回答 2查看 13.1K关注 0票数 7

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

必须有某种设置或样式才能指示WPF在GridViewColumn的可用空间中完全填充ComboBox

这是我的XAML。

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-19 21:07:18

将以下样式包含到ListViews-resources中。然后,您可以将ComboBox的HorizontalAlignment-属性设置为HorizontalAlignment="Stretch",它将如您所愿:

代码语言:javascript
复制
 <ListView.Resources>
          <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          </Style>
 </ListView.Resources>
票数 10
EN

Stack Overflow用户

发布于 2010-08-19 21:03:51

你有没有试过这个:

代码语言:javascript
复制
<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" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3521983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档