WPF DataGridTemplateColumn combobox updating all rows
Asked Answered
P

1

5

I have this XAML that selects a value from a combobox that the ItemSource is a Enum. The tutorial I used is:

http://www.c-sharpcorner.com/uploadfile/dpatra/combobox-in-datagrid-in-wpf/

            <DataGrid x:Name="dgProductItem" 
                   ItemsSource="{Binding ProductVersion.ProductItems}"

            <DataGridTemplateColumn Header="Deployment Type" Width="120">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding DeploymentType}"></TextBlock>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Source={StaticResource DeploymentTypeEnum}}"
                                  SelectedItem="{Binding DeploymentType}">

                        </ComboBox>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>

However when I change a value from one row, it will update all the rows. Does anyone know why this is?

Edit:

if I just change one row, it will only update that row, but when I go to change a different row, that row I just changed will also change the previous one..

Cheers

Pentatomic answered 25/4, 2014 at 10:49 Comment(10)
-1 For duplicating questions... even worse that you're duplicating your own question. How many questions do you want to ask about the same problem? On this site, we prefer users to edit their questions to provide more information, not ask the same question repeatedly.Hetaera
However, the answer to this question is because your DeploymentType is almost certainly singular, but you are using it for every row in the DataGrid. Your enum property needs to be in the class that each item in the DataGrid belongs to.Hetaera
I agree, its the same question but its totally different code, and editing a previous question would be pointless as will be too far down in the list. So therefore it would be an utter waste of timePentatomic
Thanks, but the enum property is in my model ProductVersion.ProductItems classPentatomic
It's not totally different code... that's how I knew it was the same question... because the code is so similar. And edits refresh the position of questions so it would have the same effect. Please read through the Help Center to see what you should and shouldn't do on this website.Hetaera
The answer is still the same... somewhere in your code, you are using one enum value that is shared between all of the items... you just need to find it.Hetaera
Ok I apologise, I didnt know it refreshed the position. I wont create same question again. well, this is what is happening, if I just change one row, it will only update one row, but when I go to change a different row, the row I just changed will also change the previous if you get me? so basically its like its doing it on selected row previousPentatomic
Where is your DeploymentType property defined?... I'm guessing that's not in your item class.Hetaera
The enum itself is in its own class with a diferrent namespace. But the property DeploymentType is defined in the ProductItem model class. So basically I can get the Deployment type property from the Itemsource of the Datagrid, thats where I think the problem liesPentatomic
Where is the duplicate? I could use the solution to this problem.Ferry
P
10

Apologies for the duplicates but after a few hours of guessing because there isn't enough material on the web for this kinda stuff, the solution is:

</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <ComboBox ItemsSource="{Binding Source={StaticResource DeploymentTypeEnum}}"
                  SelectedItem="{Binding DeploymentType}"
                  **IsSynchronizedWithCurrentItem="false**">
        </ComboBox>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

IsSynchronizedWithCurrentItem - does what it says on the tin. However, when you select an item, the current one will disappear but at least it won't update all rows.

Pentatomic answered 25/4, 2014 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.