views:

38

answers:

2

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>

Thanks for your interest.

A: 

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" />
Steve Danner
A: 

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>
HCL
Thanks. This is working fine, but its still leaving some margin between the Combo and the Cell border. Is there a way to remove that?
Amby
You can try changing the ControlTemplate: http://msdn.microsoft.com/en-us/library/ms788717.aspx
HCL
@HCL: Thanks, this link helped. Marking your answer as the solution.
Amby