Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "Fill" a GridViewColumn with ComboBox (WPF)?

I am looking for a way to "completely fill" a GridViewColumn with a combo box. I am able to create a cell template with ComboBox and it is working fine. But the width and height of ComboBox is not aligned with the GridViewColumn. Even if I try to set the same height/width GridViewColumn hides some part of the comboBox.

There must be some setting or style to instruct WPF to fill the ComboBox completely in the available space of GridViewColumn

This is my 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>
like image 755
Manish Basantani Avatar asked Aug 19 '10 12:08

Manish Basantani


People also ask

How do I add a ComboBox item in WPF?

On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.

What is ComboBox in WPF?

A combobox is a selection control that combines a non-editable textbox and a drop-down listbox that allows users to select an item from a list. It either displays the current selection or is empty if there is no selected item.


2 Answers

Include the following style into the ListViews-resources. Then you can set the HorizontalAlignment-property of the ComboBox to HorizontalAlignment="Stretch" and it will do as you wish:

 <ListView.Resources>
          <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          </Style>
 </ListView.Resources>
like image 188
HCL Avatar answered Sep 23 '22 12:09

HCL


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" />
like image 27
Steve Danner Avatar answered Sep 20 '22 12:09

Steve Danner