DataGrid Combobox IsSynchronisationWithCurrentItem=False. Cannot setup xaml

scott thomson 66 Reputation points
2020-12-15T10:31:52.367+00:00

After a few days of trial and error for about every possible xaml layout I could find I am still not quite there. The below xaml looks and works fine except for one thing everytime I make a selection in either combobox that selection propagates to all other rows in the datagrid. Whilst setting IsSynchronisationWithCurrentItem=False could be the way to stop this the only way I can get IsSynchronisationWithCurrentItem=False to take effect and get rid of this behaviour is to remove the SelectedItemBinding line from the below code. This though is not great for me as I use the SelectedItemBinding and its updatetrigger behavior to initiate code in the ViewModel. I would ideally like to retain the SelectedItem or SelectedItemBinding property and get the IsSyncWithCUrrentItem=false working and stopping all the rows from taking up the values from the newly selected item.

I have in addition to below also tried configuring the DG comboboxes using DataGridTemplateColumn.CellEditingTemplate/CellTemplate layout or the straight DataGridComboBoxColumn layout with no styles or templating.

<DataGrid x:Name="AssessHazardsDataGrid" Margin="10,25,0,20" GridLinesVisibility="None" CanUserAddRows="False"
ItemsSource="{Binding DataContext.AssessHazards, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
AutoGenerateColumns="False" Grid.Column="0" EnableRowVirtualization="True" CanUserDeleteRows="False" MinRowHeight="60" IsSynchronizedWithCurrentItem="False"
RowDetailsVisibilityMode="VisibleWhenSelected" CellStyle="{StaticResource Hazarddatagridcellstyle}" BorderBrush="Red" BorderThickness="2"
SelectedItem="{Binding DataContext.SelectedAssessHazard, Converter={cnvtr:DataGridItemConverter},RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridComboBoxColumn x:Name="HazClassCombobox" Header="Hazard Class" Width="130"
SelectedValueBinding="{Binding IDHazClass, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="IDHazClass"
SelectedItemBinding="{Binding DataContext.SelectedHazClass, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type Window}}}">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.HazClasses, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding HazClass1}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding DataContext.HazClasses, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
<Setter Property="IsSynchronizedWithCurrentItem" Value="False"/>
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock TextWrapping="Wrap" Text="{Binding HazClass1}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
Developer technologies | Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. 46851620 1 Reputation point
    2023-01-04T10:41:38.697+00:00

    @DaisyTian-1203 I have exactly the same issue, i mean that all the combo having the same old value of the current row are changed to the new value.
    The combo uses database as item source.
    The XAML is as follows:

    <Window x:Class="PagamentiWPF.Anagrafica"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:PagamentiWPF"
    mc:Ignorable="d"
    Title="Anagrafica" Height="450" Width="1024">
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="11*"/>
    <RowDefinition Height="162*"/>
    <RowDefinition Height="11*"/>
    <RowDefinition Height="6*"/>
    </Grid.RowDefinitions>
    <Menu Grid.Row="0" Grid.Column="0">
    <MenuItem Header="_File">
    <MenuItem x:Name="MenuChiudi" Header="_Chiudi" Click="MenuChiudi_Click" />
    </MenuItem>
    </Menu>
    <DataGrid Name="AnagraficaDG" Margin="0,15,0,29" AutoGenerateColumns="false" Visibility="Visible" VerticalScrollBarVisibility="Auto" CanUserAddRows="false" CanUserDeleteRows="true" Grid.Row="1" CellEditEnding="AnagraficaDG_CellEditEnding" >
    <DataGrid.Columns>
    <DataGridTextColumn Header="Id" x:Name="Id" Visibility="Visible" Binding="{Binding Id}" />
    <DataGridTextColumn Header="Cognome" Binding="{Binding Cognome}" />
    <DataGridTextColumn Header="Nome" Binding="{Binding Nome}" />
    <DataGridTextColumn Header="Cellulare" Binding="{Binding Cellulare}" />
    <DataGridTextColumn Header="Telefono casa" Binding="{Binding TelCasa}" />
    <DataGridTextColumn Header="Telefono ufficio" Binding="{Binding TelUfficio}" />
    <DataGridTextColumn Header="Indirizzo" Binding="{Binding Indirizzo}" />
    <DataGridComboBoxColumn x:Name="ComboCitta"
    Header="Città"
    DisplayMemberPath="Descrizione"
    SelectedValuePath="Id"
    SelectedValueBinding="{Binding Citta.Id}">
    <DataGridComboBoxColumn.ElementStyle>
    <Style TargetType="ComboBox">
    <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
    </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
    <Style TargetType="ComboBox">
    <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
    </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
    </DataGridComboBoxColumn>
    </DataGrid.Columns>
    </DataGrid>
    <Button Name="InserisciAnagraficaBTN" Content="Inserisci" Visibility="Visible" Width="100" Height="30" HorizontalAlignment="Left" Margin="10,0,0,0" Grid.Row="2" VerticalAlignment="Center" Click="InserisciAnagraficaBTN_Click" />
    <Button Name="EliminaAnagraficaBTN" Content="Elimina" Visibility="Visible" Width="100" Height="30" HorizontalAlignment="Left" Margin="125,0,0,0" Grid.Row="2" VerticalAlignment="Center" Click="EliminaAnagraficaBTN_Click" />
    <Button Name="RicaricaAnagraficaBTN" Content="Ricarica" Visibility="Visible" Width="100" Height="30" HorizontalAlignment="Left" Margin="240,0,0,0" Grid.Row="2" VerticalAlignment="Center" Click="RicaricaAnagraficaBTN_Click" />
    </Grid>
    </Window>

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.