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:41

wpfbind1

3-Vue détail + ComboBox

Clients = new ObservableCollection<Client>(GetClients());

Categories = new ObservableCollection<Category>(GetCategories());

 

cboCategoryClient.ItemsSource = Categories;

 

DataContext = this;     

  
  •  On définit la propriété SelectedItem du datagrid (réglée sur le property SelectedClient ici) 
  •  On définit le  DataContext du conteneur de la vue détail
  • ItemsSource de la comboBox est définie dans le code behind, dans le Xaml on définit juste {Binding}  

<DataGrid x:Name="datagridClients" Height="150" ItemsSource="{Binding Clients}" SelectedItem="{Binding SelectedClient}" Grid.Row="0" AutoGenerateColumns="False" SelectionChanged="datagridClients_SelectionChanged">     

            <DataGrid.Columns>

                <DataGridTextColumn Header="Nom" Binding="{Binding Name}"/>

                <DataGridTextColumn Header="E-mail" Binding="{Binding Email}"/>

            </DataGrid.Columns>

        </DataGrid>

 

            <StackPanel DataContext="{Binding SelectedItem, ElementName=datagridClients}" Grid.Row="1">

            <TextBlock Height="20" Width="120" Name="txtClientId" Text="{Binding Path=Id}"/>

            <TextBox Height="20" Width="120" Name="txtClientName" Text="{Binding Name}"/>

            <TextBox Height="20" Width="120" Name="txtClientEmail" Text="{Binding Email}"/>

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

                      ItemsSource="{Binding}"

                      DisplayMemberPath="Name"

                      SelectedValuePath="Id"

                      SelectedValue="{Binding CategoryId}"

                      />

        </StackPanel>

 

Variante :

Il est possible de définir en code

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

 

public MainWindow()

        {

            InitializeComponent();

 

            Clients = new ObservableCollection<Client>(GetClients());

            DataContext = this;

 

            Categories = new ObservableCollection<Category>(GetCategories());

            cboCategoryClient.ItemsSource = Categories;

            cboCategoryClient.DisplayMemberPath = "Name"; // valeur affichée

            cboCategoryClient.SelectedValuePath = "Id"; // valeur cachée(clé primaire de la table mère)

 

       public Client SelectedClient { get; set; }

 

        public ObservableCollection<Client> Clients

        {

            get;

            set;

        }

        public ObservableCollection<Category> Categories

        {

            get;

            set;

        }

 

On définit l’élément sélectionné dans l’event SelectionChanged du datagrid

private void datagridClients_SelectionChanged(object sender, SelectionChangedEventArgs e)

        {

            cboCategoryClient.SelectedValue = SelectedClient.CategoryId;

        }

 

3 – Avec CollectionViewSource

L’ItemsSource du datagrid et de la comboBox se règlent sur la View de la collectionViewSource

   public MainWindow()

        {

            InitializeComponent();

         

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

            Categories = new CollectionViewSource() { Source = GetCategories() };

 

            cboCategoryClient.ItemsSource = Categories.View;

 

            DataContext = this;     

 

       

        }

 

        public Client SelectedClient { get; set; }

 

        public CollectionViewSource Clients

        {

            get;

            set;

        }

        public CollectionViewSource Categories

        {

            get;

            set;

        }

 

    <DataGrid x:Name="datagridClients" Height="150" ItemsSource="{Binding Clients.View}" SelectedItem="{Binding SelectedClient}" Grid.Row="0" AutoGenerateColumns="False" SelectionChanged="datagridClients_SelectionChanged">

            <DataGrid.Columns>

                <DataGridTextColumn Header="Nom" Binding="{Binding Name}"/>

                <DataGridTextColumn Header="E-mail" Binding="{Binding Email}"/>

            </DataGrid.Columns>

        </DataGrid>

        <StackPanel DataContext="{Binding SelectedItem, ElementName=datagridClients}" Grid.Row="1">

            <TextBlock Height="20" Width="120" Name="txtClientId" Text="{Binding Id}"/>

            <TextBox Height="20" Width="120" Name="txtClientName" Text="{Binding Name}"/>

            <TextBox Height="20" Width="120" Name="txtClientEmail" Text="{Binding Email}"/>

 

            <!--<ComboBox Height="20" Name="cboCategoryClient"/>-->

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

                      ItemsSource="{Binding}"

                      DisplayMemberPath="Name"

                      SelectedValuePath="Id"

                      SelectedValue="{Binding CategoryId}"

                      />

        </StackPanel>

 

 

Partager cet article

Repost 0
Published by Romagny13 - dans C
commenter cet article

commentaires