question

RodAtWork avatar image
0 Votes"
RodAtWork asked ·

Why does clicking on a datagrid cause the window to flash?

I'm working on a WPF app with a colleague at work. This app has a launch panel with 7 buttons. Clicking on any of the 7 buttons causes a new window to popup. These new windows all start with a datagrid, listing the contents of 1 of 7 tables, corresponding to the 7 buttons, etc.

The datagrid is actually a customized user control, that a third colleague wrote and which is being used in several other WPF applications. We have not modified it in this application. This customized user control is on the first tab of a tab control.

I'm responsible for one of those sub-windows that pops up when a user clicks on one of those buttons. The weird thing is that when a user clicks on one of the rows in the datagrid, the window flashes. And that's the thing that's really bugging me. I cannot figure out why it's flashing. And only this one window flashes, the other 6 don't. WHY??

Here's the XAML in my window, using the custom datagrid control:

 <uc:CoreSummaryControl
     x:Name="TheCoreSummaryControl"
     Grid.Row="1"
     CenterAlignedColumns="{StaticResource CenterAlignedFields}"
     CenterAlignedHeaders="{StaticResource CenterAlignedHeaders}"
     ExtraFields="{StaticResource ExtraFields}"
     Fields="{Binding TableFields}"
     LeftAlignedColumns="{StaticResource LeftAlignedFields}"
     LeftAlignedHeaders="{StaticResource LeftAlignedHeaders}"
     OmittedFields="{StaticResource OmittedFields}"
     RightAlignedColumns="{StaticResource RightAlignedFields}"
     RightAlignedHeaders="{StaticResource RightAlignedHeaders}"
     ShowDateTimesHasDates="True" />

That XAML is identical to the other 6 windows' XAML.

Like I said, my colleague and I haven't changed the code of the CoreSummaryControl at all. I could, if you want, produce it here, but I don't see how it pertains, especially since the other 6 windows use the same control and even the same XAML to use that control. But it's that control that, when clicked on, causes my window to flash.

I believe that whatever the issue is, its related somehow to that CoreSummaryControl. But I will mention one other thing, which seems like it affects the flashing, but isn't related to CoreSummaryControl. The declaration of the tab control, on all 6 windows, includes this XAML:

 <TabControl x:Name="ViewPort" Grid.Row="1" 
     Margin="0,0,0,0"
     Background="Transparent"
     BorderThickness="0"
     SelectedIndex="{Binding ViewIndex, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
     SelectionChanged="ViewPort_SelectionChanged">

I want to draw your attention to the IsAsync=True attribute that's a part of the SelectedIndex. Out of curiosity, I removed that from my window. Once I did that, it appeared to have stopped the flashing. But WHY would it do that? And why only to my window?

wpf
· 3
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HI, I recommend not using code-behind and using MVVM consistently. If in ViewPort_SelectionChanged you execute methods asynchronously, this flashing can occur. In MVVM you can log the executing processes and also the NotifyPropertyChanged and evaluate such asynchronous processes.

0 Votes 0 · ·
RodAtWork avatar image RodAtWork PeterFleischer-3316 ·

Unfortunately, I cannot do as you suggest, although I wish I could. This app is based upon another app, the code comes from the parent app and it must stay that way.

In this particular case, the window starts on the first tab of the tab control (called ViewPort). Double clicking should eventually set the SelectedIndex to 1 (the second tab), but it doesn't. I've set a breakpoint in this code and ViewPort.SelectedIndex == 0, which doesn't make sense to me, but that's what it tells me. So, why isn't it being set to 1?

0 Votes 0 · ·

This is just getting progressively worse! If I remove the IsAsync=True, the double clicking appears to do nothing at all. If I put it back it flashes, which really irritates my boss a LOT.

0 Votes 0 · ·
RodAtWork avatar image
0 Votes"
RodAtWork answered ·

I finally found the answer to my problem. It was an issue of the datagrid, which in our case is embedded in a custom user control, being loaded more than once. The issue is explained here. Our custom user control is in a tab item of a tab control, which means it would fall victim to this problem. I used the solution on that Stack Overflow post to fix the issue.

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

kentvuong-3592 avatar image
0 Votes"
kentvuong-3592 answered ·

trust is to do what is said, not for the sake of your family and yourself to be responsible to the community Honestly, you must know how to do your job well, regardless of people, to live to live

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I apologize for getting frantic. My boss and team members were getting down on me. Not being able to discover what the problem was,, when others were demanding immediate answers, was causing a LOT of anxiety.

0 Votes 0 · ·
PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered ·

Hi, check your code. It's easy to reproduce your problem with following code:

 <Window x:Class="Window92"
         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:WpfApp1"
         mc:Ignorable="d"
         Title="Flicking TabControl" Height="450" Width="800">
   <StackPanel>
     <Label Content="{Binding ViewIndex}"/>
     <TabControl SelectedIndex="{Binding ViewIndex, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
                 SelectionChanged="ViewPort_SelectionChanged">
       <TabItem Header="TabItem 1"/>
       <TabItem Header="TabItem 2"/>
       <TabItem Header="TabItem 3"/>
     </TabControl>
   </StackPanel>
 </Window>

CodeBehind:

 Imports System.ComponentModel
 Imports System.Runtime.CompilerServices
 
 Public Class Window92
   Implements INotifyPropertyChanged
   Public Sub New()
 
     ' This call is required by the designer.
     InitializeComponent()
 
     ' Add any initialization after the InitializeComponent() call.
     Me.DataContext = Me
   End Sub
   Private Async Sub ViewPort_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
     Await Task.Delay(100)
     ViewIndex = 1
   End Sub
 
   Private _viewIndex As Integer
   Public Property ViewIndex As Integer
     Get
       Return Me._viewIndex
     End Get
     Set(value As Integer)
       Me._viewIndex = value
       OnPropChanged()
     End Set
   End Property
 
 #Region " PropertyChanged"
   Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
   Friend Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
     RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub
 #End Region
 
 End Class


· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.