IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

DataGridRow Template Add row Slower


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Développeur C#
    Inscrit en
    Avril 2011
    Messages
    348
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Développeur C#
    Secteur : Industrie

    Informations forums :
    Inscription : Avril 2011
    Messages : 348
    Par défaut DataGridRow Template Add row Slower
    Bonjour,

    Est ce quelqu'un est déjà arrivé à créer un template sur une datagridview pour que quand on ajoute une ligne, l'ajout de la ligne se fasse lentement ?
    Pour qu'on la voit bien arriver.
    Je ne parviens pas à trouver l'event qui me permettra en XAML d'intercepter cet ajout.

    Auriez-vous une idée ?

  2. #2
    Membre extrêmement actif
    Inscrit en
    Avril 2008
    Messages
    2 573
    Détails du profil
    Informations personnelles :
    Âge : 65

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 573
    Par défaut
    bonjour draco951
    Le probleme est que les "containers" des items (customises generalement dans un datatemplate) sont traites d'un "seul coup" par le Thread de Rendu (hidden Render Thread auquel nous n'avons pas acces dans l'api) et non par le Thread UI...
    Ceci peut etre observe si tu applique le style suivant à un DataGridRow sans passer par le Dispatcher :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    <Style 
                x:Key="StyleDataGridRow"
                TargetType="{x:Type DataGridRow}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="MaxHeight" Value="0"/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="DataGridRow.Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation
                                      Duration="0:0:0.50"
                                      Storyboard.TargetProperty="MaxHeight"
                                      To="60"  
                                    SpeedRatio="0.005"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
    Le seul moyen pour contourner ce probleme c'est de passer par le Dispatcher d'Application et de charger les "workitems" comme dans un LongTaskRunning.
    Ceci permet de mettre "en queue" les items et les envoyer au thread UI qui appellera le "thread de rendu" pour les afficher...avec un thread.sleep
    Le code du class data utilise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
     
    namespace WinItemsRenderThread
    {
        public class MyPerson : INotifyPropertyChanged
        {
            public MyPerson()
            {
                FName = string.Empty;
                LName = string.Empty;
     
            }
            public MyPerson(string fn, string ln):base()
            {
                FName = fn;
                LName = ln;
     
            }
            private string fname;
            public string FName
            {
                get { return fname; }
                set
                {
                    fname = value; OnPropertyChanged("FName");
                }
            }
            private string lnamer;
            public string LName
            {
                get { return lnamer; }
                set
                {
                    lnamer = value; OnPropertyChanged("LName");
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string np)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(np));
                }
     
            }
        }
        public class MyPersons : ObservableCollection<MyPerson>
        {
            private MyPerson p;
            public MyPersons()
            {
     
                for (int i = 1; i < 999; i++)
                {
                    p = new MyPerson("FNAME" + i.ToString(), "LNAME" + i.ToString());
                    this.Add(p);
                }
            }
     
        }
    }
    code xaml du winform:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
     
    <Window x:Class="WinItemsRenderThread.WinLoadDGV"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WinItemsRenderThread"
            Title="WinLoadDGV" Height="300" Width="300">
        <Window.Resources>
     
            <Style
                x:Key="styleTB"
                TargetType="TextBlock">
                <Setter Property="Foreground" Value="Transparent"/>
                <Setter Property="Background" Value="Transparent"/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation 
                                    Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" 
                                    Duration="00:00:03" 
                                    From="Red" To="Transparent" />
                            </Storyboard>
                        </BeginStoryboard>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation 
                                    Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" 
                                    Duration="00:00:03" 
                                    From="Transparent" To="DarkBlue"  />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <StackPanel>
            <StackPanel
            Orientation="Horizontal">
                <Button Content="Start"  
                Click="startButton_Click"
                Name="startButton"
                Margin="5,0,5,0"
                />
                <TextBlock 
                    Margin="10,5,0,0"
                    Text ="Item Added :  ">
                </TextBlock >
                <TextBlock 
                    Name="tbNextItem" 
                    Margin="4,5,0,0"
                    Text="0">
                </TextBlock>
            </StackPanel>
            <DataGrid 
                x:Name="dgv"
                VirtualizingStackPanel.VirtualizationMode="Standard"
                AutoGenerateColumns="False"
                ItemsSource="{Binding}" 
                >
                <DataGrid.Columns>
                    <DataGridTextColumn
                            Header="FirstName" 
                            Binding="{Binding FName}"
                           />
                    <DataGridTextColumn 
                            Header="Last Name" 
                            Binding="{Binding LName}"
                            ElementStyle="{StaticResource styleTB}"/>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
    </Window>
    code .cs du winform (avec delegate):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    using System.Threading;
     
    namespace WinItemsRenderThread
    {
        /// <summary>
        /// Logique d'interaction pour WinLoadDGV.xaml
        /// </summary>
        public partial class WinLoadDGV : Window
        {
            public delegate void NextItemDelegate();
     
            //Current number to check  
            private int num = 0;
     
            MyPersons MyList = new MyPersons();
            public WinLoadDGV()
            {
                InitializeComponent();
     
            }
            private void startButton_Click(object sender, RoutedEventArgs e)
            {
     
                    this.dgv.Items.Clear();
                    num = 0;
                    startButton.Dispatcher.BeginInvoke(
                        DispatcherPriority.Normal,
                        new NextItemDelegate(LoadNextItem));
            }
            public void LoadNextItem()
            {
                MyPerson p = MyList[num];
                this.dgv.Items.Add(p);
     
     
                this.tbNextItem.Text = num.ToString() + " " + p.FName;
     
                num += 1;
                if (num < MyList.Count)
                {
     
                    this.startButton.Dispatcher.BeginInvoke(
                           DispatcherPriority.Background,
                           new NextItemDelegate(this.LoadNextItem));
                    Thread.Sleep(250);
                }
     
            }
     
     
        }
    }
    qui j'espere fera ton bonheur...
    bon code....

Discussions similaires

  1. [SSIS] [2K8] Failed to add row to output buffer.
    Par clementratel dans le forum SSIS
    Réponses: 1
    Dernier message: 11/02/2011, 12h02
  2. Ajouter un bouton "add row" dans une datagrid
    Par mikees dans le forum Flex
    Réponses: 6
    Dernier message: 03/05/2010, 16h11
  3. DataGrid - Add Rows
    Par 0xYg3n3 dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 31/03/2009, 19h48
  4. dataset add row ne pas ajouter de clé double?
    Par gregcat dans le forum Windows Forms
    Réponses: 2
    Dernier message: 30/01/2008, 11h25
  5. [xslt] For-each dans template avec format XML (row)
    Par Steff1985 dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 17/11/2005, 11h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo