DataGridComboBoxColumn loses its content when selection changes
Asked Answered
S

1

7

When I click a cell in my DataGridComboBoxColumn the ComboBox gets visible and I can select items. When I have selected an item its visible at the top thats fine. But when the cell aka ComboBox loses its focus because I click something different in the DataGrid then there is no item/text visible anymore in the cell I have previously selected.

How can I keep that selection/selected text?

thats my code:

<DataGridComboBoxColumn
           Width="*"
           Header="Monday"
           DisplayMemberPath="SchoolclassName"
           SelectedValueBinding="{Binding SchoolclassCodeMonday}"  
           ItemsSource="{Binding Source={StaticResource ClassCodes}}">

    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>

    <DataGridComboBoxColumn.EditingElementStyle>                   
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={StaticResource ClassCodes}}" />
            <Setter Property="IsDropDownOpen" Value="True" />
        </Style>                   
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

there seems to be a solution for my problem: http://wpf.codeplex.com/Thread/View.aspx?ThreadId=46627 (scroll to the bottom) but I can not transfer the solution to my problem. Because my model setup is quite different.

SchoolclassName is a string property in Schoolclass.cs SchoolclassCodeMonday is a string property in TimeTable.cs ClassCodes aka SchoolclassCodes is a property of type ObservableCollection|Schoolclass|

Someone knows how to fix my binding?

Sealskin answered 20/6, 2010 at 20:56 Comment(1)
@to the guy who voted +1 DisplayMemberPath="SchoolclassName" the above was existing code: the below I added and now the SchoolclassCode remains in the cell when I leave the combobox. Normally you use an ID for the SelectedValuePath but my "ID" is the SchoolclassCode which is already unique. SelectedValuePath="SchoolclassName" It just works for me..., I do not have a better explanation or more technical insider knowledge but you can read here that helped me a bit: blogs.msdn.com/b/vinsibal/archive/2008/10/31/…Sealskin
G
2

I know its probably not needed anymore but maybe it will help someone else. Would your ComboBox not need to update the binding when it's changed? e.g.

SelectedValueBinding="{Binding SchoolclassCodeMonday}"

would be:

SelectedValueBinding="{Binding SchoolclassCodeMonday, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  

Also make sure you are firing a notification when the property is changed from code on your observable collection.

Guttapercha answered 28/8, 2015 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.