Bonjour à tous,
étant un adèpte d'Access et VBA, je me suis lancé dans une application autonome pour la gestion d'une base de données. Je passe donc par VisualStudio Community 2015.
Suite aux conseils d'un ami, je me suis orienté vers le WPF plutôt que Widows Forms.
J'ai suivi quelques formations en C et C# mais j'avoue que le XAML, je découvre...
J'ai donc très vite rencontré mes premiers problèmes (je pense d'ailleurs que je réfléchis trop façon Access et VBA, ce qui ne m'aide pas beaucoup...)
J'ai donc créé un nouveau projet WPF appelé TestRecordsNavigation puis j'ai créé une base de données en local Database1.

Nom : newDataBase.png
Affichages : 107
Taille : 27,9 Ko


J'ai ajouté une table TB_cars et une table TB_colors.

Nom : TB_cars.png
Affichages : 93
Taille : 9,7 Ko

Nom : TB_colors.png
Affichages : 100
Taille : 7,2 Ko


Un lien "one to many" est créé entre les deux tables:

Code T-SQL : Sélectionner tout - Visualiser dans une fenêtre à part
CONSTRAINT [FK_TB_cars_To_PK_TB_color] FOREIGN KEY ([FK_COLOR]) REFERENCES [TB_colors]([PK_COLOR])


Ensuite, sur ma MainWindow.xaml, j'ai ajouté ma source de données en sélectionnant mes 2 tables:

Nom : addSource.png
Affichages : 92
Taille : 10,1 Ko


J'ai ensuite ajouté quelques contrôles sur ma MainWindow.xaml (avec quelques commentaires dans mon code XAML) :

Nom : mainWindows.png
Affichages : 102
Taille : 5,0 Ko

Code XAML : 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
<Window x:Class="TestRecordsNavigation.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:TestRecordsNavigation"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <!-- déclaration des tables sur ma WPF-->
    <Window.Resources>
        <local:Database1DataSet x:Key="database1DataSet"/>
        <CollectionViewSource x:Key="tB_carsViewSource" Source="{Binding TB_cars, Source={StaticResource database1DataSet}}"/>
        <CollectionViewSource x:Key="tB_colorsViewSource" Source="{Binding TB_colors, Source={StaticResource database1DataSet}}"/>
    </Window.Resources>
    <!-- grid principale contenant les controls-->
    <Grid x:Name="grd_main">
        <Button x:Name="cde_exit" Content="Exit" Margin="408,274,34,0" VerticalAlignment="Top" Click="cde_exit_Click"/>
        <Button x:Name="cde_next" Content="Next" HorizontalAlignment="Left" Margin="105,274,0,0" VerticalAlignment="Top" Width="75" Click="cde_next_Click"/>
        <Button x:Name="cde_previous" Content="Previous" Margin="25,274,417,0" VerticalAlignment="Top" Click="cde_previous_Click"/>
        <!-- grid secondaire avec les controls liés à ma table TB_cars-->
        <Grid x:Name="grd_cars" DataContext="{StaticResource tB_carsViewSource}" HorizontalAlignment="Left" Margin="25,20,0,0" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label x:Name="lbl_model" Content="Model :" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" Width="60"/>
            <TextBox x:Name="txb_model" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" Height="23" Margin="3" Text="{Binding MODEL, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="150"/>
            <Label x:Name="lbl_type" Content="Type :" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" Width="60"/>
            <TextBox x:Name="txb_type" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" Height="23" Margin="3" Text="{Binding TYPE, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="150"/>
        </Grid>
        <!-- grid secondaire avec les controls liés à ma table TB_colors-->
        <Grid x:Name="grd_colors" DataContext="{StaticResource tB_colorsViewSource}" HorizontalAlignment="Left" Margin="25,137,0,0" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label x:Name="lbl_color" Content="Color :" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="60"/>
            <TextBox x:Name="txb_color" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding COLOR, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="150"/>
        </Grid>
    </Grid>
</Window>


J'ai attribué des commandes à mes boutons :
Pour le bouton "EXIT" : (jusque là, pas de problème)

Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
private void cde_exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

Jusque là, c'est presque un tutoriel...

Mais c'est avec les boutons "Previous" et "Next" que ça se complique.
Ces boutons sont sensés servir à passer d'un enregistrement à l'autre sur ma table TB_cars.

Mon MainWindows.xaml.cs ressemble à ça :

Code C# : 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
 
namespace TestRecordsNavigation
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
 
            TestRecordsNavigation.Database1DataSet database1DataSet = ((TestRecordsNavigation.Database1DataSet)(this.FindResource("database1DataSet")));
            // Load data into the table TB_cars. You can modify this code as needed.
            TestRecordsNavigation.Database1DataSetTableAdapters.TB_carsTableAdapter database1DataSetTB_carsTableAdapter = new TestRecordsNavigation.Database1DataSetTableAdapters.TB_carsTableAdapter();
            database1DataSetTB_carsTableAdapter.Fill(database1DataSet.TB_cars);
            System.Windows.Data.CollectionViewSource tB_carsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tB_carsViewSource")));
            tB_carsViewSource.View.MoveCurrentToFirst();
            // Load data into the table TB_colors. You can modify this code as needed.
            TestRecordsNavigation.Database1DataSetTableAdapters.TB_colorsTableAdapter database1DataSetTB_colorsTableAdapter = new TestRecordsNavigation.Database1DataSetTableAdapters.TB_colorsTableAdapter();
            database1DataSetTB_colorsTableAdapter.Fill(database1DataSet.TB_colors);
            System.Windows.Data.CollectionViewSource tB_colorsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tB_colorsViewSource")));
            tB_colorsViewSource.View.MoveCurrentToFirst();
        }
 
        private void cde_exit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
 
        private void cde_next_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource tB_carsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tB_carsViewSource")));
            tB_carsViewSource.View.MoveCurrentToNext();
        }
 
        private void cde_previous_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource tB_carsViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("tB_carsViewSource")));
            tB_carsViewSource.View.MoveCurrentToPrevious();
        }
    }
}


Le problème est que la couleur ne suit pas...

Merci d'avance pour votre aide.