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>
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.
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.
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>
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With