0
votes

I have two TabItems each of them containing a DataGrid and in both of them I implement INotifiyDataErrorInfo. Both DataGrids are fed by a ViewModel with an underlying model class (POCO). In the first TabItem everything works as demanded but in the second TabItem the DataGrid accepts input only in the validated fields/cells. the other/non-validated fields/cells let me input data but when the cells lost the focus the newly input characters are discarded/rejected.

What can be the reason?

The working TabItem/DataGrid:

<UserControl x:Class="ConfigTool.Controls.RawTagTabItem"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ct_ctrls="clr-namespace:ConfigTool.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<Grid>
    <ct_ctrls:CTDataGrid x:Name="tagsGrid" ItemsSource="{Binding}" GridLinesVisibility="Vertical" AlternatingRowBackground="#C3DDE5" 
                         AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" 
                         SelectionUnit="FullRow" SelectionMode="Extended" BorderThickness="3" CellEditEnding="CellEditingEnds" RowStyle="{StaticResource RawTagDataGridRow}">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="TagName" Header="Tag" Width="*" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.TagName, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}" />
            <DataGridTextColumn x:Name="TagCycle" Header="Cycle" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.Cycle, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" 
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn x:Name="TagSource" Header="Source" Width="*" 
                                Binding="{Binding Mode=TwoWay, Path=RawTag.Source, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}"
                                ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}"/>
            <DataGridTextColumn x:Name="Unassigned" Header="unassigned" Width="*" 
                                Binding="{Binding Mode=OneWay, Path=RawTag.Unassigned, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=False}"
                                ElementStyle="{StaticResource ResourceKey=textBlockUnassignedStyle}"/>
            <DataGridTemplateColumn x:Name="editTagColumn" Header="" CanUserResize="True" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <Button x:Name="btnTagDelete" Click="BtnTagDelete_Click" CommandParameter="{Binding}" Height="15" Width="15" Margin="2">
                                <Button.Content>
                                    <Image Source="../Resources/delete.png"></Image>
                                </Button.Content>
                            </Button>
                        </WrapPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </ct_ctrls:CTDataGrid>
</Grid>

The NOT working TabItem/DataGrid:

<UserControl x:Class="ConfigTool.Controls.RawNotificationTabItem"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:ct_ctrls="clr-namespace:ConfigTool.Controls"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">

<Grid>
    <ct_ctrls:CTDataGrid x:Name="notificationsGrid" ItemsSource="{Binding}" GridLinesVisibility="Vertical" AlternatingRowBackground="#C3DDE5" 
                         AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" 
                         SelectionUnit="FullRow" SelectionMode="Extended" BorderThickness="3" CellEditEnding="CellEditingEnds" RowStyle="{StaticResource NotificationDataGridRow}">
            <DataGrid.Resources>
            <Style x:Key="wordWrapStyleView" TargetType="{x:Type TextBlock}">
                <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
            <Style x:Key="wordWrapStyleEdit" TargetType="{x:Type TextBox}">
                <Setter Property="TextWrapping" Value="Wrap"/>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTemplateColumn Header="Tag">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ct_ctrls:TextWithSuggestionControl x:Name="test" DataContext="{Binding}" ItemsSource="{Binding Path=Tags}" 
                                      Value="{Binding Path=Notification.TagName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Append="False"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn Header="Nid" Binding="{Binding Mode=TwoWay, Path=Notification.Nid}"/>

            <DataGridCheckBoxColumn Header="Active" Binding="{Binding Mode=TwoWay, Path=Notification.IsActive, Converter={StaticResource StringBoolConverter}}" IsThreeState="False"/>

            <DataGridTextColumn Header="Type" Binding="{Binding Mode=TwoWay, Path=Notification.Type}"/>

            <DataGridTextColumn Header="Limit" Binding="{Binding Mode=TwoWay, Path=Notification.Limit}">
                <Validation.ErrorTemplate>
                    <ControlTemplate>
                        <StackPanel>
                            <AdornedElementPlaceholder x:Name="Limit"/>
                            <TextBlock Text="{Binding [0].ErrorContent}" Background="Red"/>
                        </StackPanel>
                    </ControlTemplate>
                </Validation.ErrorTemplate>
            </DataGridTextColumn>

            <DataGridTemplateColumn Header="Operator">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ComboBox x:Name="notificationOp" DataContext="{Binding}" ItemsSource="{Binding Path=Operations}" 
                                          Text="{Binding Path=Notification.Op}" IsEditable="False" VerticalAlignment="Top"
                                          SelectedItem="{Binding Path=Notification.Op, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
                            </ComboBox>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn Header="Text" Binding="{Binding Mode=TwoWay, Path=Notification.Text}"/>

            <DataGridTextColumn Header="Deadband" Binding="{Binding Mode=TwoWay, Path=Notification.Deadband}">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="TextBlock">
                        <Setter Property="HorizontalAlignment" Value="Right" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <DataGridTextColumn Header="Delay" ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}"
                                Binding="{Binding Mode=TwoWay, Path=Notification.Delay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}">
                <DataGridTextColumn.CellStyle>
                    <Style >
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="TextBlock.TextAlignment" Value="Right"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

            <DataGridTextColumn Header="Expand" ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}" 
                                Binding="{Binding Mode=TwoWay, Path=Notification.Expand, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}">
                <DataGridTextColumn.CellStyle>
                    <Style >
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="TextBlock.TextAlignment" Value="Right"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

            <DataGridTextColumn Header="SampleCount" ElementStyle="{StaticResource ResourceKey=textBlockErrStyle}" 
                                Binding="{Binding Mode=TwoWay, Path=Notification.SampleCount, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}">
                <DataGridTextColumn.CellStyle>
                    <Style >
                        <Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch"/>
                        <Setter Property="TextBlock.TextAlignment" Value="Right"/>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

            <DataGridTemplateColumn Header="DetailTemplate">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ct_ctrls:TextWithSuggestionControl x:Name="test" DataContext="{Binding}" ItemsSource="{Binding Path=DetailTemplates}" 
                                       Value="{Binding Path=Notification.DetailTemplate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Append="False"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTemplateColumn Header="DetailParams" Width="300" >
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Grid>
                            <ct_ctrls:TextWithSuggestionControl x:Name="test" DataContext="{Binding}" ItemsSource="{Binding Path=Tags}" 
                                  Value="{Binding Path=Notification.DetailParamsString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Append="True"/>
                        </Grid>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            <DataGridTextColumn Header="unassigned" 
                                Binding="{Binding Mode=OneWay, Path=Notification.Unassigned, ValidatesOnDataErrors=False, NotifyOnValidationError=False}"
                                ElementStyle="{StaticResource ResourceKey=textBlockUnassignedStyle}"/>

            <DataGridTemplateColumn x:Name="EditNotificationsColumn" Header="">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <WrapPanel>
                            <Button x:Name="btnNotificationDelete" Click="BtnNotificationDelete_Click" Height="15" Width="15" Margin="2">
                                <Image Source="../Resources/delete.png"></Image>
                            </Button>
                        </WrapPanel>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </ct_ctrls:CTDataGrid>
</Grid>

The DataGrids are filled after reading CSV files well...only trying to modify the (non-validated) cells (which are in fact DataGridTextColumns and DataGridCheckboxColumns) causes the problem.

1
Try deleting the data grid and adding a new one.preciousbetine
Programmatically or do you mean just delete the source file and create a new file maybe because there are some invalid/invisible characters or so?du-it
I deleted the file (.xaml and .xaml.cs) and recreated them...without success. Still the same behaviour.du-it

1 Answers

0
votes

It's pretty easy: Do it correctly and it works! ;-) I just forgot to add UpdateSourceTrigger=PropertyChanged in the binding of the cells which rejected the input.