MVVM行为ScrollView错误加载

时间:2016-06-22 14:23:02

标签: c# wpf mvvm

在我的项目中,我有一种行为来检测ScrollView在我的数据网格中的底部。

只有当数据从视图加载加载并且需要直接使用ScrollView时,他才能很好地工作。

有时我的数据可以第二次加载而不是。

我的行为

public class ScrollViewerMonitor
{
    public static DependencyProperty AtEndCommandProperty
        = DependencyProperty.RegisterAttached(
            "AtEndCommand", typeof(ICommand),
            typeof(ScrollViewerMonitor),
            new PropertyMetadata(OnAtEndCommandChanged));

    public static ICommand GetAtEndCommand(DependencyObject obj)
    {
        return (ICommand)obj.GetValue(AtEndCommandProperty);
    }

    public static void SetAtEndCommand(DependencyObject obj, ICommand value)
    {
        obj.SetValue(AtEndCommandProperty, value);
    }


    public static void OnAtEndCommandChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)d;
        if (element != null)
        {
            element.Loaded -= element_Loaded;
            element.Loaded += element_Loaded;
        }
    }

    static void element_Loaded(object sender, RoutedEventArgs e)
    {
        FrameworkElement element = (FrameworkElement)sender;
        element.Loaded -= element_Loaded;
        ScrollViewer scrollViewer = FindChildOfType<ScrollViewer>(element);
        if (scrollViewer == null)
        {
            return;
        }

        var dpd = DependencyPropertyDescriptor.FromProperty(ScrollViewer.VerticalOffsetProperty, typeof(ScrollViewer));
        dpd.AddValueChanged(scrollViewer, delegate (object o, EventArgs args)
        {
            bool atBottom = scrollViewer.VerticalOffset
                           >= scrollViewer.ScrollableHeight;
            if (atBottom)
            {
                var atEnd = GetAtEndCommand(element);
                if (atEnd != null)
                {
                    atEnd.Execute(null);
                }
            }
        });
    }

    static T FindChildOfType<T>(DependencyObject root) where T : class
    {
        var queue = new Queue<DependencyObject>();
        queue.Enqueue(root);

        while (queue.Count > 0)
        {
            DependencyObject current = queue.Dequeue();
            for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
            {
                var child = VisualTreeHelper.GetChild(current, i);
                var typedChild = child as T;
                if (typedChild != null)
                {
                    return typedChild;
                }
                queue.Enqueue(child);
            }
        }
        return null;
    }
}

在我的XAML中

<D:DataGridTemplate  x:Name="Datagrid" Behavior:ScrollViewerMonitor.AtEndCommand="{Binding LoadCommand,UpdateSourceTrigger=PropertyChanged}">

我希望即使在启动时没有出现滚动视图,行为也可以检测到滚动视图何时出现

此Datagrid不起作用:

            <D:DataGridTemplate  x:Name="Datagrid" Behavior:ScrollViewerMonitor.AtEndCommand="{Binding LoadCommand}" SelectedValue="{Binding SelectedID}" SelectedValuePath="Swmo_id" Grid.Column="0"  Grid.Row="1" Grid.RowSpan="1"  ItemsSource= "{Binding TypeMouvement, Converter={StaticResource TypeCollectionConverter},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.ColumnSpan="2" >
            <!--"{Binding TypeMouvement, Converter={StaticResource TypeCollectionConverter}}"TypeMouvement-->
            <DataGrid.Resources>
                <Helper:BindingProxy x:Key="proxy" Data="{Binding}" />
            </DataGrid.Resources>
            <DataGrid.Columns>
                <D:DataGridFirstColumn Visibility="{Binding Data.TypeMouvement, Converter={StaticResource FirstColumnConverter}, Source={StaticResource proxy}}"/>
                <!--Mofi_Id-->
                <DataGridTextColumn Header="N° Chrono" Width="Auto"  Binding="{Binding Path=Mofi_Id, Converter={StaticResource GestionMouvementConverter}, ConverterParameter=Mofi_Id}">
                    <DataGridTextColumn.Visibility>
                        <MultiBinding  Converter="{StaticResource  ColumnTypeConverter}" Mode="TwoWay">
                            <MultiBinding.Bindings>
                                <Binding  Path="Data.DataChampFinal[0].Cham_Order" Source="{StaticResource proxy}" />
                                <Binding  Path="Data.TypeMouvement" Source="{StaticResource proxy}" />
                            </MultiBinding.Bindings>
                        </MultiBinding>
                    </DataGridTextColumn.Visibility>
                </DataGridTextColumn>
                <!--id-->

                <DataGridTextColumn Header="ID"   Binding="{Binding Path=Swmo_id}" 
                                    Visibility="{Binding Data.DataChampFinal[1].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />

                <!--Visibility="{Binding Data.Swmo_id, Converter={StaticResource VisibleColumnConverter}, ConverterParameter=1, Source={StaticResource proxy} "{Binding coucou, Converter={StaticResource DisplayIndexOrder}, ConverterParameter=0}" DisplayIndex="{Binding Int10, Mode = TwoWay, FallbackValue = 1}" Visibility="{Binding Data.TypeMouvement, Converter={StaticResource ColumnTypeConverter}, Source={StaticResource proxy}}"  />  -->
                <DataGridTextColumn Header="Date de validation"  Binding="{Binding Path=Mofi_Datefinal, Converter={StaticResource GestionMouvementConverter}, ConverterParameter=Mofi_Datefinal}">
                    <DataGridTextColumn.Visibility>
                        <MultiBinding  Converter="{StaticResource  ColumnTypeConverter}" Mode="TwoWay">
                            <MultiBinding.Bindings>
                                <Binding  Path="Data.DataChampFinal[2].Cham_Order" Source="{StaticResource proxy}" />
                                <Binding  Path="Data.TypeMouvement" Source="{StaticResource proxy}" />
                            </MultiBinding.Bindings>
                        </MultiBinding>
                    </DataGridTextColumn.Visibility>
                </DataGridTextColumn>

              <DataGridTextColumn Header="Date du mouvement" Binding="{Binding Path=Mobr_Datemouv, Converter={StaticResource GestionMouvementConverter}, ConverterParameter=ConvertDateFR }"
                                    Visibility="{Binding Data.DataChampFinal[3].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Date d'Entrée CM." Binding="{Binding Path=Mobr_Dateentreecm, Converter={StaticResource GestionMouvementConverter}, ConverterParameter=ConvertDateFR }"
                                    Visibility="{Binding Data.DataChampFinal[4].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Entrepôt Fiscal"   Binding="{Binding Path=Mobr_Identrepotfiscal}" 
                                    Visibility="{Binding Data.DataChampFinal[5].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" /> 
                <DataGridTextColumn Header="Entrepôt Compte"   Binding="{Binding Path=Mobr_Identrcpt}" 
                                    Visibility="{Binding Data.DataChampFinal[6].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Compte"  Binding="{Binding Path=Comp_Id}"  
                                    Visibility="{Binding Data.DataChampFinal[7].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Couleur" Binding="{Binding Path=Comp_Idcouleur}" 
                                    Visibility="{Binding Data.DataChampFinal[8].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Sous Compte"    Binding="{Binding Path=Mobr_Idsouscompte}"  
                                    Visibility="{Binding Data.DataChampFinal[9].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Type mouvement" Binding="{Binding Path=Mobr_Mouvement}" 
                                    Visibility="{Binding Data.DataChampFinal[10].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Type justificatif si ecrit auto." Width="Auto"  Binding="{Binding Path=Mobr_Idtypeecrit}" 
                                    Visibility="{Binding Data.DataChampFinal[11].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Code Produit"   Binding="{Binding Path=Prod_Id}" 
                                    Visibility="{Binding Data.DataChampFinal[12].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Degré"  Binding="{Binding Path=Mobr_Degré}" 
                                    Visibility="{Binding Data.DataChampFinal[13].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Code Mouvement"   Binding="{Binding Path=Como_Id}" 
                                    Visibility="{Binding Data.DataChampFinal[14].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Entrée/sortie"   Binding="{Binding Path=Mobr_TypeMvt}"  
                                    Visibility="{Binding Data.DataChampFinal[15].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Sens"   Binding="{Binding Path=Mobr_Sens}"  
                                    Visibility="{Binding Data.DataChampFinal[16].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Type de Tiers du Mvt."     Binding="{Binding Path=Mobr_Idtierstyp}" 
                                    Visibility="{Binding Data.DataChampFinal[17].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Code du Tiers" Binding="{Binding Path=Mobr_Codetiers}"  
                                    Visibility="{Binding Data.DataChampFinal[18].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Désignation du Tiers"      Binding="{Binding Path=Mobr_Nomtiers}" 
                                    Visibility="{Binding Data.DataChampFinal[19].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Type de Pièce Gestion"     Binding="{Binding Path=Mobr_Idpiecegest}" 
                                    Visibility="{Binding Data.DataChampFinal[20].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Référence Pièce Origine1"  Binding="{Binding Path=Mobr_Refpiece1}"  
                                    Visibility="{Binding Data.DataChampFinal[21].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                ...
                <DataGridTextColumn Header="CRA" Binding="{Binding Path=Mobr_Cra}"  
                                    Visibility="{Binding Data.DataChampFinal[26].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Type Nature Expédition"    Binding="{Binding Path=Mobr_Idexptype}"  
                                    Visibility="{Binding Data.DataChampFinal[27].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Qte. régie en L."          Binding="{Binding Path=Mobr_Qteregielitre}" 
                                    Visibility="{Binding Data.DataChampFinal[28].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Vol. effectif en L."       Binding="{Binding Path=Mobr_Effectiflitre}"  
                                    Visibility="{Binding Data.DataChampFinal[29].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Qte. Gest. Commerciale en U." Binding="{Binding Path=Mobr_Qtegestcom}"  
                                    Visibility="{Binding Data.DataChampFinal[30].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Libre5"  Binding="{Binding Path=Mobr_Libre5}"    
                                    Visibility="{Binding Data.DataChampFinal[40].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />
                <DataGridTextColumn Header="Libre6"  Binding="{Binding Path=Mobr_Libre6}"    
                                    Visibility="{Binding Data.DataChampFinal[41].Cham_Order, Mode=TwoWay, Converter={StaticResource VisibleColumnConverter}, Source={StaticResource proxy}}" />

            </DataGrid.Columns>
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Historique..." Command="{Binding GoSeeHistory}" />
                </ContextMenu>
            </DataGrid.ContextMenu>
        </D:DataGridTemplate>

这一个很好:

       <D:DataGridTemplate  x:Name="Datagrid" 
                         Behavior:ScrollViewerMonitor.AtEndCommand="{Binding LoadCommand}"  
                         TablePaged:SortColumn.AtSortingColumnCommand="{Binding SortingColumnCommand}" 
                         Grid.Column="0"  Grid.Row="1" Grid.RowSpan="2"  ItemsSource="{Binding DataProduits,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
        <DataGrid.Resources>
            <Helper:BindingProxy x:Key="proxy" Data="{Binding}" />
            <CollectionViewSource x:Key="Currentcollection" Source="{Binding Path= ColumnName}" />

        </DataGrid.Resources>

        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding Path=Prod_Id}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>

            <DataGridTextColumn Header="Désignation" Binding="{Binding Path=Prod_Des}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Des" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Désignation court" Binding="{Binding Path=Prod_Lib}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Lib" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Millésime" Binding="{Binding Path=Prod_Millesime}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Millesime" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Type" Binding="{Binding Path=Prod_Type}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Type" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Centilisation" Binding="{Binding Path=Prod_centilisation}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_centilisation" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Appellation" Binding="{Binding Path=Prod_IdAppel}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_IdAppel" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Produit de base" Binding="{Binding Path=Prod_Idpdb}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idpdb" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Couleur" Binding="{Binding Path=Prod_Idcouleur}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idcouleur" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Ref. Douane" Binding="{Binding Path=Prod_Idrefdouane}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idrefdouane" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Catégorie" Binding="{Binding Path=Prod_Idcategorie}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idcategorie" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Regie" Binding="{Binding Path=Prod_Idprodregie}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idprodregie" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Statut regie" Binding="{Binding Path=Prod_Idsteap}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Idsteap" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                    </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Ste. d'appartenance" Binding="{Binding Path=Prod_Cloture}">
                <DataGridTextColumn.Visibility>
                    <Binding Path="Data.ColumnName" ConverterParameter="Prod_Cloture" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" Source="{StaticResource proxy}" >
                        <Binding.Converter>
                            <Converters:VisibilityColumn CurrentCollection="{StaticResource Currentcollection}"  />
                        </Binding.Converter>
                 </Binding>
                </DataGridTextColumn.Visibility>
            </DataGridTextColumn>


        </DataGrid.Columns>


    </D:DataGridTemplate>

0 个答案:

没有答案