Simple editable DataGridComboBoxColumn not working

Feb 9, 2009 at 3:23 PM
Edited Feb 9, 2009 at 3:24 PM
Hi there,

Perhaps I'm doing something stupid, but I cannot get a simple editable DataGridComboBoxColumn to work. Basically, the combo side of things works OK, but once the focus is lost it loses the text. I have tried binding both Text in both the ElementStyle and EditableElementStyle to no avail.

Are there any samples for this? I can email a small sample that shows the problem if needs be.

Thanks,
Kent

PS. sorry for double post. I got an error and assumed it didn't work.
Feb 11, 2009 at 10:56 AM
*bump* Anybody?
Coordinator
Feb 11, 2009 at 1:05 PM
How is your ItemsSource on the DataGridComboBoxColumn instantiated?
Feb 11, 2009 at 2:54 PM
Here's the XAML for my Window:

<Window x:Class="EditableComboBoxRepro.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wpf="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
    Title="Editable ComboBox Repro"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Window.Resources>
        <col:ArrayList x:Key="Values">
            <sys:String>1</sys:String>
            <sys:String>5</sys:String>
            <sys:String>10</sys:String>
            <sys:String>-1</sys:String>
            <sys:String>-5</sys:String>
            <sys:String>-10</sys:String>
        </col:ArrayList>
    </Window.Resources>
    
    <wpf:DataGrid ItemsSource="{Binding NameValuePairs}" AutoGenerateColumns="False" CanUserAddRows="False">
        <wpf:DataGrid.Columns>
            <wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <wpf:DataGridComboBoxColumn Header="Value (Setting Properties in Styles)">
                <wpf:DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="Background" Value="{Binding Background}"/>
                        <Setter Property="Text" Value="{Binding Value}"/>
                        <Setter Property="IsEditable" Value="True"/>
                        <Setter Property="ItemsSource" Value="{Binding Source={StaticResource Values}}"/>
                    </Style>
                </wpf:DataGridComboBoxColumn.ElementStyle>
                <wpf:DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="Background" Value="{Binding Background}"/>
                        <Setter Property="IsEditable" Value="True"/>
                        <Setter Property="ItemsSource" Value="{Binding Source={StaticResource Values}}"/>
                        <Setter Property="Text" Value="{Binding Value}"/>
                    </Style>
                </wpf:DataGridComboBoxColumn.EditingElementStyle>
            </wpf:DataGridComboBoxColumn>
        </wpf:DataGrid.Columns>
    </wpf:DataGrid>
</Window>

As you can see, I have tried different approaches to get it to work. No dice.

Best,
Kent
Coordinator
Feb 11, 2009 at 7:53 PM
I'm not sure what the issue is from just looking at that but have you also tried setting TextBinding on DataGridComboBoxColumn? 
Feb 11, 2009 at 9:18 PM
Yes, that's the first thing I tried :)

It looks to me as though the bindings are being cleared or something, but I haven't had time to debug through the code to verify.

Is there somewhere I can email my repro?

Thanks,
Kent
Mar 3, 2009 at 2:14 PM
I have the same problem.
Does anybody know how to solve this?

Thanks in advance.
Mar 3, 2009 at 4:06 PM
hello,

I think I found the solution:
you have to add:

<

 

Setter Property="IsSynchronizedWithCurrentItem" Value="False" />

This works for me.

 

Mar 4, 2009 at 9:26 AM
afva,

Unfortunately, your workaround seems to do nothing for me.

Best,
Kent
Oct 5, 2009 at 10:49 AM

hello,

is there a solution for this issue? Because I have the same problem right now.

Greetings

 

Oct 14, 2009 at 1:20 PM

Hi I had problems with this issue al well. I did find a working example. I searched to find any differences with my code and I found out that it worked if you set the SelectedValueBinding and SelectedValuePath properties as well (as the SelectedItemBinding).

So this

<Controls:DataGridComboBoxColumn DisplayMemberPath="DisplayName"
                                 Header="Truck" Width="200" 
                                 SelectedItemBinding="{Binding SelectedTruck }"
                                 >
    <Controls:DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
            <Setter Property="ItemsSource" Value="{Binding Path=Trucks}" />
        </Style>
    </Controls:DataGridComboBoxColumn.ElementStyle>
    <Controls:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=Trucks}" />
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>
    </Controls:DataGridComboBoxColumn.EditingElementStyle>
</Controls:DataGridComboBoxColumn>
 
does not work but this
 
<Controls:DataGridComboBoxColumn DisplayMemberPath="DisplayName"
                                 Header="Truck" Width="200" 
                                 SelectedValueBinding="{Binding SelectedTruck.Key}"
                                 SelectedValuePath="Key"
                                 SelectedItemBinding="{Binding SelectedTruck }"
                                 >
    <Controls:DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
            <Setter Property="ItemsSource" Value="{Binding Path=Trucks}" />
        </Style>
    </Controls:DataGridComboBoxColumn.ElementStyle>
    <Controls:DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Path=Trucks}" />
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>
    </Controls:DataGridComboBoxColumn.EditingElementStyle>
</Controls:DataGridComboBoxColumn>
does work and doesn't loses the selected item when focus is lost.
I am not 100% sure why this is ... Maybe someone can elaborate ?
Nov 29, 2009 at 5:48 PM

hi, Beretta1979. I also have this problem. Help me, please

What is 

SelectedTruck

in your code?

Thank you!

 

Nov 30, 2009 at 10:39 AM
_tanusha_ wrote:

hi, Beretta1979. I also have this problem. Help me, please

What is 

SelectedTruck

in your code?

Thank you!

 

 That is just a property of my viewmodel representing a truck object. (the selected one in the combobox out of the list of trucks)

Feb 1, 2012 at 9:06 PM

Your not doing anything stupid, the amount of work it takes to get a DataGridComboBoxColumn working is absolutely staggering

Feb 1, 2012 at 11:35 PM

Best resource on DataGridComboBoxColumn I found is the example here:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcomboboxcolumn.aspx