Binding listview Collection d'objets
Bonjour,
j'ai toujours quelques soucis avec le binding.
voilà, j'ai
classe NumeroLotto
Code:
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.ComponentModel;
namespace LottoVerification
{
class NumeroLotto : INotifyPropertyChanged
{
int numero;
bool tire;
public NumeroLotto(int p_numero, bool p_tire)
{
numero = p_numero;
tire = p_tire;
}
public NumeroLotto()
{
numero = 0;
tire = false;
}
public int Numero
{
set
{
numero = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Numero"));
}
}
get
{
return numero;
}
}
public bool Tire
{
set
{
tire = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Tire"));
}
}
get
{
return tire;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
} |
une classe Joueur
Code:
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace LottoVerification
{
class Joueur : INotifyPropertyChanged
{
private bool continuer;
private string nom;
public ObservableCollection<NumeroLotto> numeros = new ObservableCollection<NumeroLotto>();
public Joueur(string p_nom, int p_num1, int p_num2, int p_num3, int p_num4, int p_num5, int p_num6)
{
nom = p_nom;
numeros.Add(new NumeroLotto(p_num1, false));
numeros.Add(new NumeroLotto(p_num2, false));
numeros.Add(new NumeroLotto(p_num3, false));
numeros.Add(new NumeroLotto(p_num4, false));
numeros.Add(new NumeroLotto(p_num5, false));
numeros.Add(new NumeroLotto(p_num6, false));
}
public Joueur()
{
nom = "";
numeros.Add(new NumeroLotto());
numeros.Add(new NumeroLotto());
numeros.Add(new NumeroLotto());
numeros.Add(new NumeroLotto());
numeros.Add(new NumeroLotto());
numeros.Add(new NumeroLotto());
}
public string Nom
{
set
{
nom = value;
continuer = true;
if (String.IsNullOrEmpty(value))
{
continuer = false;
throw new ApplicationException();
}
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
}
}
get
{
return nom;
}
}
public bool Continuer
{
get
{
return continuer;
}
set
{
continuer = value;
}
}
public void trierNumeros()
{
NumeroLotto numTmp = new NumeroLotto();
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 6; j++)
{
if (i != j)
{
if (this.numeros[i].Numero < this.numeros[j].Numero)
{
numTmp = this.numeros[i];
this.numeros[i] = this.numeros[j];
this.numeros[j] = numTmp;
}
}
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
} |
et ma fenêtre principale
XAML
Code:
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
|
<Window x:Class="LottoVerification.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="600" Width="800" Loaded="Window_Loaded">
<Window.Resources>
<ResourceDictionary>
<Style x:Key="HeaderStyle" TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Visibility" Value="Collapsed" />
</Style>
<Style x:Key="RowStyle" TargetType="ListViewItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border BorderBrush="Green"
BorderThickness="1"
CornerRadius="20"
Height="40"
Width="500"
Background="LightGreen"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="3">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<GridViewRowPresenter Content="{TemplateBinding Content}"
Columns="{TemplateBinding GridView.ColumnCollection}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsMouseOver" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border BorderBrush="Red"
BorderThickness="1"
CornerRadius="20"
Height="40"
Width="500"
Background="Pink"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="3">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<GridViewRowPresenter Content="{TemplateBinding Content}"
Columns="{TemplateBinding GridView.ColumnCollection}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
<RowDefinition Height="40"/>
</Grid.RowDefinitions>
<Menu Grid.Row="0">
<MenuItem Header="Joueurs">
<MenuItem Click="MenuItem_Click" Header="Ajouter"/>
</MenuItem>
</Menu>
<ListView Grid.Row="1" Name="listJoueurs" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource RowStyle}">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<Binding Path="Nom" UpdateSourceTrigger="PropertyChanged"></Binding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Path=numeros}" DataContext="joueurs">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Numero}"></TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button Content="Supprimer joueurs" Click="Button_Click" Grid.Row="2"></Button>
</Grid>
</Window> |
Behind
Code:
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.Collections.ObjectModel;
namespace LottoVerification
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
XmlReader readerXml = new XmlReader();
public Window1()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
AjoutJoueur a = new AjoutJoueur();
a.ShowDialog();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
XmlReader reader = new XmlReader();
reader.DeleteAllJoueurs();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<Joueur> joueurs;
joueurs = readerXml.GetAllJoueurs();
listJoueurs.DataContext = joueurs;
}
}
} |
Le but de ma fenêtre principale est d'afficher une collection d'objets Joueur, collection récupérée dans un fichier XML.
Je veux afficher ces objets via une listview.
J'arrive bien à afficher la propriété Nom de mes joueurs.
mais je n'arrive pas à afficher ma collection de NumeroLotto qui appartient à mon joueur.
il ne me met pas d'erreur, mais il ne m'affiche rien.
Quelqu'un aurait une idée.
Un grand merci.