0

I have a wpf dadatagrid and every time the contents being loaded the dgDataGrid_Loaded event triggers twice. I am not sure whether this is WPF bug or I have missed something!?

here is my Xaml code:

<my:DataGrid Name="dgDataGrid" DockPanel.Dock="Top"
         AutoGenerateColumns="False"  ClipboardCopyMode="ExcludeHeader"                                              
         CanUserDeleteRows="True"
         SelectionMode="Extended"  SelectionUnit="FullRow"                              
         FontFamily="Tahoma"
         ItemsSource="{Binding}" 
         VirtualizingStackPanel.VirtualizationMode="Recycling"
         VirtualizingStackPanel.IsVirtualizing="True"
         EnableRowVirtualization="false"
         EnableColumnVirtualization="False"
         IsSynchronizedWithCurrentItem="True"
         BorderBrush="Blue" 
         RowBackground="White"
         HorizontalGridLinesBrush="Blue" 
         GridLinesVisibility="Horizontal" VerticalGridLinesBrush="Blue" IsTextSearchEnabled="False" 
         IsTabStop="True" HeadersVisibility="All"
         Loaded="dgDataGrid_Loaded" ContextMenuOpening="dgDataGrid_ContextMenuOpening"
         LoadingRow="dgDataGrid_LoadingRow">
            <my:DataGrid.Resources>

            </my:DataGrid.Resources>

            <my:DataGrid.RowHeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type my:DataGridRow}}, Path=Header}"></TextBlock>
                </DataTemplate>
            </my:DataGrid.RowHeaderTemplate>
            <my:DataGrid.ColumnHeaderStyle>
                <Style TargetType="my:DataGridColumnHeader">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Text="{Binding}" Foreground="Blue"/>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </my:DataGrid.ColumnHeaderStyle>

            <my:DataGrid.ContextMenu>
                <ContextMenu Name="cmDataGrid" StaysOpen="True">
                    <MenuItem Name="mnuView" Header="نمایش">
                        <MenuItem Name="mnuHideColumn"  Header="Hide Column" Click="mnuHideColumn_Click"/>
                        <MenuItem Name="mnuShowColumn" Header="Show Column"/>
                        <Separator/>
                        <MenuItem Name="mnuGroupByColumn" 
                          Header="Group by this column" Click="mnuGroupColumn_Click" />
                        <MenuItem Name="mnuClearGroups" 
                          Header="Clear grouping" Click="mnuGroupColumn_Click" />
                        <Separator/>
                        <MenuItem Header="Header Alignment">
                            <MenuItem Name="mnuHeaderCenter" Header="Center"/>
                            <MenuItem Name="mnuHeaderLeft" Header="Left"/>
                            <MenuItem Name="mnuHeaderRight" Header="Right"/>
                        </MenuItem>
                        <MenuItem Header="Content Alignment">
                            <MenuItem Name="mnuContentCenter" Header="Center"/>
                            <MenuItem Name="mnuContentLeft" Header="Left"/>
                            <MenuItem Name="mnuContentRight" Header="Right"/>
                        </MenuItem>
                    </MenuItem>

                </ContextMenu>
            </my:DataGrid.ContextMenu>
        </my:DataGrid>

Any suggestions?

amir moradifard
  • 353
  • 1
  • 8
  • 26
  • I don't think you'ved missed anything, nor do I think it would generally be considered a bug. Events that are not generated by the user (ie Loaded, Initialized) are usually pretty annoying to depend on in WPF because the framework assumes you are creating controls in a declarative, rather than procedural way. It's been my experience that fighting WPF by trying to do procedural modifications only leads to a bad cycle of bug fixing/creating. What are you trying to accomplish? There is probably a more WPF-ish solution that won't be as error prone. – Andrew Apr 24 '13 at 06:15
  • Thanks @Andrew.I am doing several things in dgDataGrid_Loaded but the problem is it is being hit twice and therefore some calculations return wrong value. The problem is that this does not happen just for dgDataGrid_Loaded, it happens for i.e. dgScrollbar_ScrollChanged as well. I need this to load another batch of data whenever the scrollbar reaches the last row as a custom pagination. However I have created a test project and add a simple Datagrid and appended mock data to it, and this time dgDataGrid_Loaded hit once! So I assume I have missed something in my main project. – amir moradifard Apr 24 '13 at 06:34
  • I would guess that there is something else in your UI that is causing the DataGrid to get refreshed, and thus causing the Loaded event to fire more than once. Even if you tackle the issue at hand, I would be very concerned that some future change to your code would cause this issue to rear its head again. You could always use a simple flag in your handler to make sure you only take action once per instance. (or better yet, un-sign up the handler inside the handler) – Andrew Apr 24 '13 at 07:17
  • Yes the Flag is the first solution coming to mind, but I need to find the source of problem as this problem will slowing down the application. I will trace it and will update the thread if I found the source. Thanks again – amir moradifard Apr 24 '13 at 07:21

0 Answers0