Bonjour à tous,
Etant débutant en WPF, je me suis fixé un challenge, faire un jeu de carte basique en WPF.
Ce jeu de carte devra entre autre intégrer des images, et du drag en drop.
J'ai donc une classe, "Carte" qui représente mon objet, une classe CartePanel, qui hérite de panel, et qui représente mon viewModel.
les 2 Classes ci-dessous :
Classe carte :
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
62
63
64
65
66
67
68
69
70
71
72
73 public class Carte : DependencyObject { public Carte(int annee, string pictureName) { Annees = annee; image = new BitmapImage(new Uri("pack://application:,,,/TimeLinePresentation;component/Assets/" + pictureName)); EnMain = true; } public int Annees { get; set; } public bool EnMain { get { return (bool)GetValue(EnMainProperty); } set { SetValue(EnMainProperty, value); SetValue(EnPiocheProperty, !value); SetValue(EnLigneProperty, !value); } } public bool EnPioche { get { return (bool)GetValue(EnPiocheProperty); } set { SetValue(EnPiocheProperty, value); SetValue(EnMainProperty, !value); SetValue(EnLigneProperty, !value); } } public bool EnLigne { get { return (bool)GetValue(EnLigneProperty); } set { SetValue(EnLigneProperty, value); SetValue(EnMainProperty, !value); SetValue(EnPiocheProperty, !value); } } // Using a DependencyProperty as the backing store for EnMain. This enables animation, styling, binding, etc... public static readonly DependencyProperty EnMainProperty = DependencyProperty.Register("EnMain", typeof(bool), typeof(Carte), new PropertyMetadata(true)); // Using a DependencyProperty as the backing store for EnPioche. This enables animation, styling, binding, etc... public static readonly DependencyProperty EnPiocheProperty = DependencyProperty.Register("EnPioche", typeof(bool), typeof(Carte), new PropertyMetadata(true)); // Using a DependencyProperty as the backing store for EnLigne. This enables animation, styling, binding, etc... public static readonly DependencyProperty EnLigneProperty = DependencyProperty.Register("EnLigne", typeof(bool), typeof(Carte), new PropertyMetadata(true)); private ImageSource image; public ImageSource Image { get { return image; } set { image = value; } } }
Classe CartePanel :
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 public class CartePanel : Panel { // Using a DependencyProperty as the backing store for CardHeight. This enables animation, styling, binding, etc... public static readonly DependencyProperty CardHeightProperty = DependencyProperty.Register("CardHeight", typeof(double), typeof(CartePanel), new PropertyMetadata(50D)); // Using a DependencyProperty as the backing store for CardWidth. This enables animation, styling, binding, etc... public static readonly DependencyProperty CardWidthProperty = DependencyProperty.Register("CardWidth", typeof(double), typeof(CartePanel), new PropertyMetadata(100D)); // Using a DependencyProperty as the backing store for OffsetX. This enables animation, styling, binding, etc... public static readonly DependencyProperty OffsetXProperty = DependencyProperty.Register("OffsetX", typeof(double), typeof(CartePanel), new PropertyMetadata(1D)); // Using a DependencyProperty as the backing store for OffsetY. This enables animation, styling, binding, etc... public static readonly DependencyProperty OffsetYProperty = DependencyProperty.Register("OffsetY", typeof(double), typeof(CartePanel), new PropertyMetadata(5D)); public double OffsetY { get { return (double)GetValue(OffsetYProperty); } set { SetValue(OffsetYProperty, value); } } public double OffsetX { get { return (double)GetValue(OffsetXProperty); } set { SetValue(OffsetXProperty, value); } } public double CardHeight { get { return (double)GetValue(CardHeightProperty); } set { SetValue(CardHeightProperty, value); } } public double CardWidth { get { return (double)GetValue(CardWidthProperty); } set { SetValue(CardWidthProperty, value); } } protected override Size MeasureOverride(Size availableSize) { Size resultSize = new Size(0, 0); foreach (UIElement child in Children) { child.Measure(new Size(CardWidth, CardHeight)); resultSize.Width = Math.Max(resultSize.Width, child.DesiredSize.Width); resultSize.Height = Math.Max(resultSize.Height, child.DesiredSize.Height); } return resultSize; } }
Dans mon XAML, j'ai instancié un ItemsControl, comme ci-dessous :
et là, mon xaml.cs
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 <Window x:Class="TimeLinePresentation.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:TimeLineDTO="clr-namespace:TimeLineDTO;assembly=TimeLineDTO" Title="MainWindow" Height="568" Width="931.333" ResizeMode="NoResize"> <Grid HorizontalAlignment="Left" Height="539" Margin="0,0,0,-1" VerticalAlignment="Top" Width="921" ClipToBounds="True"> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="21*"/> <ColumnDefinition Width="209*"/> <ColumnDefinition Width="691*"/> </Grid.ColumnDefinitions> <TextBox HorizontalAlignment="Left" Height="23" Margin="63,10,0,0" Grid.Row="2" TextWrapping="Wrap" Text="Points :" VerticalAlignment="Top" Width="46" Grid.Column="1" RenderTransformOrigin="1.698,0.572"/> <TextBox HorizontalAlignment="Left" Height="72" Margin="10,57,0,0" Grid.Row="2" TextWrapping="Wrap" Text="Points affichés" VerticalAlignment="Top" Width="210" Grid.ColumnSpan="2"/> <!--Definition de la main--> <ItemsControl Grid.Row="2" Grid.Column="2" Margin="0,0,0,10" ItemsSource="{Binding main}" x:Name="Main" DragLeave="Main_DragLeave"> <ItemsControl.DataContext> <TimeLineDTO:CartePanel/> </ItemsControl.DataContext> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <WrapPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <Image Source="{Binding Path=Image}" Height="110" Width="75"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid > </Window>
Comportement actuel :
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 namespace TimeLinePresentation { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public ObservableCollection<Carte> pioche; public ObservableCollection<Carte> main; public ObservableCollection<Carte> enLigne; public MainWindow() { InitializeComponent(); DataContext = this; main = new ObservableCollection<Carte>(); pioche = new ObservableCollection<Carte>(); enLigne = new ObservableCollection<Carte>(); } private void btn_Pioche_Click(object sender, RoutedEventArgs e) { Carte carte2013 = new Carte(2013, "2013.jpg"); main.Add(carte2013); } private void Main_DragLeave(object sender, DragEventArgs e) { } } }
lorsque j'appuie sur mon bouton, mon objet Carte est bien istancié, et ajouté à mon ObservableCollection, mais rien ne s'affiche dans mon UI.
Comportement attendu :
Avoir l'image de ma carte qui s'affiche dans la collection.
A priori, je pense avoir un soucis avec le xaml, dans l'itemsControl, j'ai l'impression qu'il ne trouve pas ma propriété Image.
J'ai pourtant bien défini mon DataContext (dans mon Xaml).
Je continue à chercher de mon coté, aussi, si vous avez des pistes, n'hésitez pas à m'en faire part !
Error.
Partager