Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridComboBoxColumn loses its content when selection changes

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?

like image 520
msfanboy Avatar asked Jun 20 '10 20:06

msfanboy


Video Answer


1 Answers

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.

like image 60
Cadogi Avatar answered Sep 19 '22 14:09

Cadogi