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 :

WPF : Traduire du XAML en code Behind


Sujet :

Windows Presentation Foundation

  1. #1
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut WPF : Traduire du XAML en code Behind
    Comment puis-je reproduire en code-behind C# le XAML suivant ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
        <Window.Resources>
            <local:DataSetDBWpf x:Key="dataSetDBWpf"/>
            <CollectionViewSource x:Key="clientsViewSource" Source="{Binding Clients, Source={StaticResource dataSetDBWpf}}"/>
            <CollectionViewSource x:Key="clientCommandesViewSource" Source="{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}"/>
        </Window.Resources>
    En fait, je cherche un moyen d'instancier les deux CollectionViewSource dans le code C#

    Merci d'avance de votre aide
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  2. #2
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    ajouter une resource sur le window :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    this.resources.add(key, value)
    instancier un collectionviewsource, je serais tenter de faire un new dessus ^^
    après tu lui remplies sa propriété source

    pour instancier un binding c'est new binding, donc genre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    var b = new binding("Clients");
    b.source = this.resources["dataSetDBWpf"]; // recherche de ressource, de mémoire s'il ne trouve là où tu la demandes (this ici) il cherche en amont (application)
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  3. #3
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Pour le cas de la CollectionViewSource x:Key="clientCommandesViewSource" comment initialiser la propriété Source pour qu'elle soit égale à "{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}" ?
    Merci beaucoup
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  4. #4
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    Deja répondu, essaye d'écrire le code ...
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  5. #5
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    J'ai essayé avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    var b = new Binding("Clients");
    b.Source = this.Resources("dataSetDBWpf");
    J'obtiens une erreur à la compilation qui dit : CS1955 C# Non-invocable member cannot be used like a method.
    Qu'est-ce qui pourrait manquer pour que cela compile ?
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  6. #6
    Expert éminent sénior Avatar de Pol63
    Homme Profil pro
    .NET / SQL SERVER
    Inscrit en
    Avril 2007
    Messages
    14 154
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : .NET / SQL SERVER

    Informations forums :
    Inscription : Avril 2007
    Messages : 14 154
    Points : 25 072
    Points
    25 072
    Par défaut
    oui, j'ai mis des parenthèses au lieu des crochets
    resources est un dictionary

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    b.Source = this.Resources["dataSetDBWpf"];
    Cours complets, tutos et autres FAQ ici : C# - VB.NET

  7. #7
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Avec ce code, je me retrouve avec l'ensemble des clients affiché dans le DataGrid Détail à la place des commandes.
    Ce n'est bien sûr pas ce que j'attends en résultat.
    Par ailleurs, les deux DataGrid Maitre et Détail sont désynchronisés.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Binding bdg = new Binding("dataRelationClientCommandes");
    bdg.Source = clientsViewSource.Source;
     
    clientCommandesViewSource.Source = bdg.Source;
    Nom : Output.jpg
Affichages : 612
Taille : 21,3 Ko

    D'autres pistes pour initialiser correctement une CollectioViewSource de type de celle en exemple ?
    Merci de votre aide
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  8. #8
    Membre chevronné
    Homme Profil pro
    edi
    Inscrit en
    Juin 2007
    Messages
    898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 898
    Points : 1 915
    Points
    1 915
    Par défaut
    Ce code :

    Citation Envoyé par star Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Binding bdg = new Binding("dataRelationClientCommandes");
    bdg.Source = clientsViewSource.Source;
     
    clientCommandesViewSource.Source = bdg.Source;
    revient à :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    clientCommandesViewSource.Source = clientsViewSource.Source;
    Il me paraît donc normal que ta liste de de commande affiche une liste de clients, puisque tu lui fournit une liste de clients.

  9. #9
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    C'est que je ne vois pas du tout comment procéder dans le code behind pour synchroniser ces deux CollectionViewSources dans une configuration Maitre/Détails.

    J'ai essayé ceci :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    clientsViewSource.Source = dataSetDBWpf.Clients.DefaultView;
    clientCommandesViewSource.Source = dataSetDBWpf.Clients.ChildRelations["dataRelationClientCommandes"].ChildTable.DefaultView;
    Et cela donne :

    Nom : Output.jpg
Affichages : 599
Taille : 45,3 Ko

    Le DataGrid Détails n'est plus synchronisé avec l'enregistrement courant du DataGrid Maitre. Il affiche toutes les commandes de tous les clients.

    Comment faire pour rétablir la synchronisation ?

    Merci d'avance de vos conseils
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  10. #10
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Est-ce que quelqu'un pourrait m'aider SVP ?
    Merci
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  11. #11
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    J'avoue n'avoir jamais utilisé de collectionViewSource, sauf peut etre pour avoir une liste de valeur en xaml.
    Pourquoi tu bindes pas le datasource de tes commandes sur le selectedItem.TaProprieteDeNavigationCommande de tes clients ?

  12. #12
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Je veux bien essayer, sauf que je ne sais pas de quelle manière il faut procéder pour le définir dans le XAML ou le code behind.

    Mon XAML

    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
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    <Window x:Class="DBWpf.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:DBWpf"
            mc:Ignorable="d"
            Title="MainWindow" Height="561.4" Width="933.2" Loaded="Window_Loaded">
        <Window.Resources>
            <local:DataSetDBWpf x:Key="dataSetDBWpf"/>
            <CollectionViewSource x:Key="clientsViewSource" Source="{Binding Clients, Source={StaticResource dataSetDBWpf}}"/>
            <CollectionViewSource x:Key="clientCommandesViewSource" Source="{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}"/>
        </Window.Resources>
     
        <DockPanel x:Name="mainDockPanel" LastChildFill="False" VerticalAlignment="Top">
     
            <ToolBarTray DockPanel.Dock="Top" Background="White">
                <ToolBar Band="1" BandIndex="1">
                    <Button Click="firstRecordButton_Click">
                        <Image Source="Resources/First_48x.png"/>
                    </Button>
                    <Button Click="previousRecordButton_Click">
                        <Image Source="Resources/Previous_48x.png"/>
                    </Button>
                    <Button Click="nextRecordButton_Click">
                        <Image Source="Resources/Next_48x.png"/>
                    </Button>
                    <Button Click="lastRecordButton_Click">
                        <Image Source="Resources/Last_48x.png"/>
                    </Button>
                    <Button Click="insertRecordButton_Click">
                        <Image Source="Resources/Insert_48x.png"/>
                    </Button>
                    <Button Click="deleteRecordButton_Click">
                        <Image Source="Resources/Delete_48x.png"/>
                    </Button>
                    <Button Click="clientsBindingNavigatorSaveItem_Click">
                        <Image Source="Resources/Save_48x.png"/>
                    </Button>
                </ToolBar>
            </ToolBarTray>
     
            <ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="471">
                <!-- IsDeferredScrollingEnabled="True" -->
     
                <Grid DataContext="{StaticResource clientsViewSource}" Margin="10" Width="895" VerticalAlignment="Top">
     
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition />
                    </Grid.RowDefinitions>
     
     
                    <DataGrid x:Name="clientsDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Grid.Column="0" Grid.Row="0" Margin="0,20,3,0" RowDetailsVisibilityMode="VisibleWhenSelected" Height="171.2" VerticalAlignment="Top" UseLayoutRounding="False" ScrollViewer.CanContentScroll="True" AlternatingRowBackground="LightGray" GotFocus="clientsDataGrid_GotFocus">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="client_IDColumn" Binding="{Binding Client_ID}" Header="Client ID" IsReadOnly="True" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="nomColumn" Binding="{Binding Nom}" Header="Nom" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="prenomColumn" Binding="{Binding Prenom}" Header="Prenom" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="telephoneColumn" Binding="{Binding Telephone}" Header="Telephone" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="commentaireColumn" Binding="{Binding Commentaire}" Header="Commentaire" Width="SizeToHeader"/>
                            <DataGridCheckBoxColumn x:Name="actifColumn" Binding="{Binding Actif}" Header="Actif" Width="SizeToHeader"/>
                            <DataGridCheckBoxColumn x:Name="residentColumn" Binding="{Binding Resident}" Header="Resident" Width="SizeToHeader"/>
                            <DataGridCheckBoxColumn x:Name="non_ResidentColumn" Binding="{Binding Non_Resident}" Header="Non Resident" Width="SizeToHeader"/>
                            <DataGridCheckBoxColumn x:Name="etrangerColumn" Binding="{Binding Etranger}" Header="Etranger" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="rangColumn" Binding="{Binding Rang}" Header="Rang" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="niveauColumn" Binding="{Binding Niveau}" Header="Niveau" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="centresColumn" Binding="{Binding Centres}" Header="Centres" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="serviceColumn" Binding="{Binding Service}" Header="Service" Width="SizeToHeader"/>
                            <DataGridTemplateColumn x:Name="date_NaissanceColumn" Header="Date Naissance" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Date_Naissance, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn x:Name="stampColumn" Header="Stamp" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Stamp, Mode=OneWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
     
                    <DataGrid x:Name="commandesDataGrid"  AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding Source={StaticResource clientCommandesViewSource}}" Grid.Column="0" Grid.Row="1" Margin="0,20,3,0" RowDetailsVisibilityMode="VisibleWhenSelected" Height="200" VerticalAlignment="Top" UseLayoutRounding="False" ScrollViewer.CanContentScroll="True" AlternatingRowBackground="LightGray" GotFocus="commandesDataGrid_GotFocus">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="commande_IDColumn" Binding="{Binding Commande_ID}" Header="Commande ID" IsReadOnly="True" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="client_IDColumn1" Binding="{Binding Client_ID}" Header="Client ID" IsReadOnly="False" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="libelleColumn" Binding="{Binding Libelle}" Header="Libelle" Width="SizeToHeader"/>
                            <DataGridTemplateColumn x:Name="date_CommandeColumn" Header="Date Commande" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Date_Commande, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn x:Name="stampColumn1" Header="Stamp" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Stamp, Mode=OneWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
     
                </Grid>
     
            </ScrollViewer>
     
        </DockPanel>
     
    </Window>
    Peux-tu me conseiller sur la marche à suivre ?
    Merci d'avance
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  13. #13
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    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
            <CollectionViewSource x:Key="clientCommandesViewSource" Source="{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}"/>
    ...
    ..
                    <DataGrid x:Name="commandesDataGrid"  AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding ElementName=clientsDataGrid, Path=SelectedItem.LaRelationDesCommandes}" Grid.Column="0" Grid.Row="1" Margin="0,20,3,0" RowDetailsVisibilityMode="VisibleWhenSelected" Height="200" VerticalAlignment="Top" UseLayoutRounding="False" ScrollViewer.CanContentScroll="True" AlternatingRowBackground="LightGray" GotFocus="commandesDataGrid_GotFocus">
                        <DataGrid.Columns>
                            <DataGridTextColumn x:Name="commande_IDColumn" Binding="{Binding Commande_ID}" Header="Commande ID" IsReadOnly="True" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="client_IDColumn1" Binding="{Binding Client_ID}" Header="Client ID" IsReadOnly="False" Width="SizeToHeader"/>
                            <DataGridTextColumn x:Name="libelleColumn" Binding="{Binding Libelle}" Header="Libelle" Width="SizeToHeader"/>
                            <DataGridTemplateColumn x:Name="date_CommandeColumn" Header="Date Commande" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Date_Commande, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn x:Name="stampColumn1" Header="Stamp" Width="SizeToHeader">
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker SelectedDate="{Binding Stamp, Mode=OneWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
     ....
    Bien sur c'est si chaque client a bien une propriété LaRelationDesCommandes qui te donne la liste des commandes du client

  14. #14
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    En fait, le fond de ma question n'est pas de trouver une autre manière de définir une liaison Maitre/Détails par le XAML mais plutôt d'écrire les instructions qui le fassent dans le code behind.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    <CollectionViewSource x:Key="clientsViewSource" Source="{Binding Clients, Source={StaticResource dataSetDBWpf}}"/>
    <CollectionViewSource x:Key="clientCommandesViewSource" Source="{Binding dataRelationClientCommandes, Source={StaticResource clientsViewSource}}"/>
    Je cherche à savoir : Comment écrire le code C# équivalent au XAML précédent ?

    Merci de vos conseils
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  15. #15
    Expert confirmé
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Novembre 2009
    Messages
    2 025
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Novembre 2009
    Messages : 2 025
    Points : 5 462
    Points
    5 462
    Par défaut
    Peut etre comme cela :
    https://stackoverflow.com/questions/...in-code-in-wpf

    Mais je comprends pas qu'elle est ton besoin plus large.

  16. #16
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Merci micka pour tes suggestions, cela me permets d'avancer
    Je vais tester ta proposition dès que j'aurais un moment dans la journée
    Pour te permettre de mieux comprendre mes intentions, je te propose de lire ce poste qui est en lien direct avec celui-ci : https://www.developpez.net/forums/d1...olidation-7-a/

    Edit :

    J'ai essayé avec ça :

    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
     
                    CollectionViewSource cvsClients = new CollectionViewSource();
                    Binding bdgClients = new Binding();
                    bdgClients.Path = new PropertyPath("dataSetDBWpf");
                    bdgClients.Source = dataSetDBWpf.Clients;
                    bdgClients.Mode = BindingMode.TwoWay;
                    bdgClients.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);
                    clientsViewSource = cvsClients;
     
                    CollectionViewSource cvsCommandes = new CollectionViewSource();
                    Binding bdgCommandes = new Binding();
                    bdgCommandes.Path = new PropertyPath("dataRelationClientCommandes");
                    // bdg.Source = (CollectionViewSource)App.Current.MainWindow.FindResource("clientsViewSource");
                    bdgCommandes.Source = clientsViewSource; // ((CollectionViewSource)(FindResource("clientsViewSource")));
                    bdgCommandes.Mode = BindingMode.TwoWay;
                    bdgCommandes.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    BindingOperations.SetBinding(cvsCommandes, CollectionViewSource.SourceProperty, bdgCommandes);
                    clientCommandesViewSource = cvsCommandes;
    Mais bon, ça me donne une erreur lorsque je clique par ailleurs :

    An unhandled exception of type 'System.NullReferenceException' occurred in DBWpf.exe
    Additional information: La référence d'objet n'est pas définie à une instance d'un objet.
    J'avoue que ce n'est pas très concluant et je ne sais pas si je m'y prends vraiment bien à vrai dire.
    .
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  17. #17
    Membre chevronné
    Homme Profil pro
    edi
    Inscrit en
    Juin 2007
    Messages
    898
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : edi

    Informations forums :
    Inscription : Juin 2007
    Messages : 898
    Points : 1 915
    Points
    1 915
    Par défaut
    Dans la CollectionViewSource des commandes tu devrais sans doute te binder sur le CurrentItem de la propriété View de la CollectionViewSource des clients, afin de récupérer les commandes du client sélectionné.

  18. #18
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    J'ai essayé sans la redéfinition du binding de la CollectionViewSource de la vue Clients, avec donc seulement celle de la vue Commandes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    CollectionViewSource cvsCommandes = new CollectionViewSource();
    Binding bdgCommandes = new Binding();
    bdgCommandes.Path = new PropertyPath("dataRelationClientCommandes");
    bdgCommandes.Source = clientsViewSource; // bdgCommandes.Source = clientsViewSource.View.CurrentItem;
    bdgCommandes.Mode = BindingMode.TwoWay;
    bdgCommandes.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(cvsCommandes, CollectionViewSource.SourceProperty, bdgCommandes);
    clientCommandesViewSource = cvsCommandes;
    Et cela fonctionne apparamment.
    Idem, avec ta proposition, pas de problème, cela ne lui fait ni chaud ni froid.

    Donc, j'ai un problème avec ma définition du binding de la CollectionViewSource de la vue Clients. J'ai tenté en inversant la Source et le Path :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    CollectionViewSource cvsClients = new CollectionViewSource();
    Binding bdgClients = new Binding();
    bdgClients.Path = new PropertyPath("Clients");
    bdgClients.Source = dataSetDBWpf;
    bdgClients.Mode = BindingMode.TwoWay;
    bdgClients.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);
    clientsViewSource = cvsClients;
    J'obtiens cette erreur d'exécution :

    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
    Additional information: Une liaison TwoWay ou OneWayToSource ne peut pas fonctionner sur la propriété en lecture seule 'Clients' de type 'DBWpf.DataSetDBWpf'.
    Je sèche !!!
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  19. #19
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Ok j'ai essayé de cette façon :

    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
    CollectionViewSource cvsClients = new CollectionViewSource();
    Binding bdgClients = new Binding();
    bdgClients.Path = new PropertyPath("Clients");
    bdgClients.Source = dataSetDBWpf;
    BindingOperations.SetBinding(cvsClients, CollectionViewSource.SourceProperty, bdgClients);
    clientsViewSource = cvsClients;
    clientsViewSource.View.CurrentChanged += new System.EventHandler(this.bindingsourceClients_CurrentChanged);
    datarowCurrentClient = ((DataRowView)(clientsViewSource.View.CurrentItem)).Row;
    clientsDataGrid.DataContext = clientsViewSource;
    clientsDataGrid.ItemsSource = clientsViewSource.View;
     
    CollectionViewSource cvsCommandes = new CollectionViewSource();
    Binding bdgCommandes = new Binding();
    bdgCommandes.Path = new PropertyPath("dataRelationClientCommandes");
    bdgCommandes.Source = clientsViewSource.Source;
    BindingOperations.SetBinding(cvsCommandes, CollectionViewSource.SourceProperty, bdgCommandes);
    clientCommandesViewSource = cvsCommandes;
    clientCommandesViewSource.View.CurrentChanged += new System.EventHandler(bindingsourceCommandes_CurrentChanged);
    datarowCurrentCommande = ((DataRowView)(clientCommandesViewSource.View.CurrentItem)).Row;
    commandesDataGrid.DataContext = clientsViewSource;
    commandesDataGrid.ItemsSource = clientCommandesViewSource.View;
    When I execute the code, I am getting this result :

    Nom : Before sync.jpg
Affichages : 562
Taille : 19,1 Ko

    Before adding a new customer and his order :

    Nom : Before sync 2.jpg
Affichages : 551
Taille : 24,6 Ko

    Before saving to the datasource :

    Nom : Before sync 3.jpg
Affichages : 555
Taille : 18,4 Ko

    After saving :

    Nom : After sync.jpg
Affichages : 543
Taille : 19,9 Ko

    When changing the current customer :

    Nom : After sync 2.jpg
Affichages : 544
Taille : 20,1 Ko

    Le DataGrid détails des commandes ne se synchronise plus correctement avec le client courant du DataGrid maître.

    Pourriez-vous m'indiquer ce que j'aurais oublié dans la définition des liaisons ?

    Merci de votre aide
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

  20. #20
    Membre éprouvé Avatar de star
    Homme Profil pro
    .
    Inscrit en
    Février 2004
    Messages
    840
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Corée Du Nord

    Informations professionnelles :
    Activité : .

    Informations forums :
    Inscription : Février 2004
    Messages : 840
    Points : 980
    Points
    980
    Par défaut
    Une idée ?
    .
    Diviser c'est régner : United we stand, Divided we fall
    .

Discussions similaires

  1. WPF - Modifier couleur bouton XAML dans le code-behind
    Par telbas dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 28/12/2016, 17h58
  2. XAMLReader.Load() et XAML code-behind.
    Par sacoum dans le forum Windows Presentation Foundation
    Réponses: 21
    Dernier message: 19/12/2011, 14h20
  3. XAML - Récupérer une instance créée en code-behind
    Par jacquesprogram dans le forum Windows Presentation Foundation
    Réponses: 7
    Dernier message: 09/09/2009, 15h35
  4. WPF, Win32 app, XAML et code-behind?
    Par sacoum dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 27/05/2008, 10h06

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