Overblog Suivre ce blog
Editer l'article Administration Créer mon blog

Présentation

  • : Romagny13 - Du .NET,du pur .NET
  • Romagny13 - Du .NET,du pur .NET
  • : Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
  • Contact

Recherche

Articles Récents

2 février 2014 7 02 /02 /février /2014 01:42

binding0.png

(Les méthodes GetClients() et GetCategories() retournent une liste générique)

        public MainWindow()

        {

            InitializeComponent();

 

            Clients = new CollectionViewSource() { Source = GetClients() };

            cboCategoryClient.ItemsSource = GetCategories();

 

            this.DataContext = Clients;

            this.View = (CollectionView)(CollectionViewSource.GetDefaultView(this.Clients.View));

        }

 

        public CollectionViewSource Clients { get; set; }

        private CollectionView View;

1-Navigation

IsSynchronizedWithCurrentItem="True"(du datagrid) permet de synchroniser avec l’item courant. Il est possible aussi de le gérer par code .

  private void btnFirst_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            this.View.MoveCurrentToFirst();

        }

 

        private void btnPrevious_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            if ((this.View.CurrentPosition > 0))

            {

                this.View.MoveCurrentToPrevious();

            }

        }

 

        private void btnNext_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            if ((this.View.CurrentPosition < (this.View.Count - 1)))

            {

                this.View.MoveCurrentToNext();

            }

        }

 

        private void btnLast_Click(object sender, System.Windows.RoutedEventArgs e)

        {

            this.View.MoveCurrentToLast();

        }

 

        // Lorsqu'on sélectionne une ligne dans le datagrid, l'élément est affiché dans la vue détail

        private void dgClients_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            View.MoveCurrentTo(dgClients.SelectedItem);

        }

        // synchronise l'élément sélectionné dans le datagrid avec l'élément affiché dans la vue détail lors de la navigation

        void View_CurrentChanged(object sender, EventArgs e)

        {

            dgClients.SelectedItem = View.CurrentItem;

        }

2-Tri

private void btnSort_Click(object sender, RoutedEventArgs e)

        {

            //Clients.View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

            //ou

            View.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));

        }

3-Filtre

private void btnFilter_Click(object sender, RoutedEventArgs e)

        {

            // Clients.View.Filter = FilterClient;

            // Clients.View.Filter = new Predicate<object>(Client => ((Client)Client).Name.Contains("B"));

            // OU

            //View.Filter = FilterClient;

                View.Filter = new Predicate<object>(Client => ((Client)Client).Name.ToLower().Contains(txtFilter.Text.ToLower().Trim()));

 

        }       

        // Filtre

        private bool FilterClient(object item)

        {

            Client client = item as Client;

 

            return client.Name.Contains(txtFilter.Text);

        }

     

Xaml

<StackPanel>

        <StackPanel>

            <Label Name="lblId" Height="23" Content="{Binding Id}"/>

            <TextBox Name="txtName" Height="23" TextWrapping="Wrap" Text="{Binding Name}"/>

            <TextBox Name="txtEmail" Height="23" TextWrapping="Wrap"  Text="{Binding Email}"/>

            <ComboBox Height="20" Name="cboCategoryClient"

                      ItemsSource="{Binding}"

                      DisplayMemberPath="Name"

                      SelectedValuePath="Id"

                      SelectedValue="{Binding CategoryId}"

                      />

        </StackPanel>

        <StackPanel Orientation="Horizontal">

            <Button HorizontalAlignment="Left" Name="btnFirst" Width="36" Height="25" VerticalAlignment="Top" Click="btnFirst_Click">|&lt;</Button>

            <Button HorizontalAlignment="Left" Name="btnPrevious" Width="36" Height="25" VerticalAlignment="Top" Click="btnPrevious_Click">&lt;</Button>

            <Button HorizontalAlignment="Left" Name="btnNext"  Width="36" Height="25" VerticalAlignment="Top" Click="btnNext_Click">&gt;</Button>

            <Button HorizontalAlignment="Left"  Name="btnLast" Width="36" Height="25" VerticalAlignment="Top" Click="btnLast_Click">&gt;|</Button>

            <TextBox Name="txtFilter" Width="100"/>

            <Button Name="btnFilter" Content="Filtrer"  Click="btnFilter_Click"/>

            <Button x:Name="btnSort" Content="Trier"  Click="btnSort_Click" />                

        </StackPanel>

        <StackPanel>

            <DataGrid Name="dgClients" ItemsSource="{Binding}" SelectionChanged="dgClients_SelectionChanged" Height="208"/>

        </StackPanel>

    </StackPanel>

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires