Grid view PropertyChanged invoked only after unfocusing/unselecting the current cell.

0 Answers 49 Views
GridView
Matvii
Top achievements
Rank 1
Matvii asked on 08 Nov 2024, 11:04 AM | edited on 08 Nov 2024, 11:06 AM

I need to be notified every time my value in a column changes. But it invokes PropertyChanged only after I unfocus/unselected the cell I was editing. I've tried your grid view example
https://docs.telerik.com/devtools/wpf/controls/radgridview/getting-started/getting-started2 and it has the same functionality even if I'm setting Mode=TwoWay, UpdateSourceTrigger=PropertyChanged it still triggers ProperyChanged only after I make changes and unselect the current cell. I want to trigger the notify event every time I change.
My grid view:
<telerik:RadGridView x:Name="IdentityProvidersGridView"
                     ItemsSource="{Binding IdentityProviders}"
                     SelectedItem="{Binding SelectedIdentityProvider}"
                     Visibility="{Binding IsChecked, ElementName=EnableTokenValidation, Converter={StaticResource BoolToVis}}"
                     SelectionMode="Single"
                     ShowGroupPanel="False"
                     NewRowPosition="Bottom"
                     GroupRenderMode="Flat"
                     CanUserInsertRows="True"
                     AutoGenerateColumns="False"
                     CanUserReorderColumns="False"
                     RowIndicatorVisibility="Collapsed"
                     AddingNewDataItem="AddIdentityProviderItem"
                     PreviewKeyDown="DataGrid_PreviewKeyDown"
                     Margin="0,10,0,10" Grid.Row="10">

 

                <telerik:RadGridView.Columns>
<telerik:GridViewCheckBoxColumn Header="Enable"
                                        EditTriggers="CellClick"
                                        AutoSelectOnEdit="True"
                                        Width="Auto"
                                        DataMemberBinding="{Binding EnableProvider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<telerik:GridViewCheckBoxColumn.CellStyle>
<Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</telerik:GridViewCheckBoxColumn.CellStyle>
</telerik:GridViewCheckBoxColumn>

 

                    <telerik:GridViewDataColumn Header="Internal provider ID"
                                    DataMemberBinding="{Binding ProviderId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    EditTriggers="CellClick" Width="Auto"/>

 

                    <telerik:GridViewDataColumn x:Name="InternalProviderLabelColumn" Header="Internal provider label"
                                    DataMemberBinding="{Binding SchemeName,  Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"
                                    EditTriggers="CellClick" Width="Auto"/>

 

                    <telerik:GridViewDataColumn Header="Token provider URL"
                                    DataMemberBinding="{Binding Issuer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                    EditTriggers="CellClick" Width="*"/>

 

                    <telerik:GridViewDataColumn Header="Actions" Width="Auto">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<telerik:RadButton Content="X" Click="DeleteIdentityProvider_Click"
                                                   CommandParameter="{Binding}" Width="50" HorizontalAlignment="Center"/>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>

        private void OnProviderPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            MarkDirtyIfStateChanged();
        }

public class IdentityProvider : INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

Matvii
Top achievements
Rank 1
commented on 08 Nov 2024, 04:44 PM

The solution that I found is to use GridViewDataColumn.CellTemplate and DataTemplate to add notification directly toTextBox, but then my SelectedItem is not updating 
<telerik:RadGridView x:Name="IdentityProvidersGridView"
                     ItemsSource="{Binding IdentityProviders}"
                     SelectedItem="{Binding SelectedIdentityProvider}"
                     Visibility="{Binding IsChecked, ElementName=EnableTokenValidation, Converter={StaticResource BoolToVis}}"
                     SelectionMode="Single"
                     ShowGroupPanel="False"
                     NewRowPosition="Bottom"
                     GroupRenderMode="Flat"
                     CanUserInsertRows="True"
                     AutoGenerateColumns="False"
                     CanUserReorderColumns="False"
                     RowIndicatorVisibility="Collapsed"
                     AddingNewDataItem="AddIdentityProviderItem"
                     PreviewKeyDown="DataGrid_PreviewKeyDown"
                     Margin="0,10,0,10" Grid.Row="10">

<telerik:GridViewDataColumn x:Name="InternalProviderLabelColumn" Header="Internal provider label"
                DataMemberBinding="{Binding SchemeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                EditTriggers="CellClick" Width="Auto">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding SchemeName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
Martin Ivanov
Telerik team
commented on 12 Nov 2024, 12:34 PM

Indeed, the UpdateSourceTrigger setting of the DataMemberBinding is not propagated to the default editor. To achieve your requirement you can use the cell template. However, in your case I think the CellEditTemplate is more suitable than the CellTemplate. This way you should be able to keep the selection intact, which otherwise is stolen by the focus give to the TextBox element (in the CellTemplate) on mouse left button down.
Matvii
Top achievements
Rank 1
commented on 12 Nov 2024, 03:04 PM

Thanks, Martyn that's working, I was using Cell Template in combination with  CellLoaded where I was adding event handlers through the left button by clicking "AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler). But for my checkbox now I need to click twice to be able to check/uncheck it if I'm setting GridViewDataColumn and removing DataMemberBinding then by default my checkbox is invisible(I'm using a Windows 11 theme and my background is white, probably this may be a reason) only after I click on it I see it.
How can I use the checkbox in my grid and click only one time to check/uncheck I need to select an item which checkbox I'm currently checking/unchecking(same functionality as for textbox in cellEditTemplate)
<telerik:GridViewCheckBoxColumn Header="Enable"
                    DataMemberBinding="{Binding EnableProvider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    EditTriggers="CellClick" Width="Auto">
<telerik:GridViewCheckBoxColumn.CellEditTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding EnableProvider, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</telerik:GridViewCheckBoxColumn.CellEditTemplate>
<telerik:GridViewCheckBoxColumn.CellStyle>
<Style TargetType="telerik:GridViewCell" BasedOn="{StaticResource GridViewCellStyle}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</telerik:GridViewCheckBoxColumn.CellStyle>
</telerik:GridViewCheckBoxColumn>
Martin Ivanov
Telerik team
commented on 15 Nov 2024, 11:56 AM

This happens because you need minimum of one interaction with the cell (when EditTriggers=CellClick) in order to enter its edit mode. To achieve your requirement you can get back to your original approach and move the CheckBox in the CellTemplate. To make sure that the SelectedItem of the GridView will get updated, you can subscribe to the Click event of the CheckBox in the CellTemplate and manually select the parent row of the clicked CheckBox. In the Click event handler, you can get the data item via the DataContext property of the CheckBox.
Matvii
Top achievements
Rank 1
commented on 15 Nov 2024, 12:47 PM

I will try, thanks Martin

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Matvii
Top achievements
Rank 1
Share this question
or