Binding de la propriété ItemsSource d'un DataGridComboBoxColumn
Bonjour à tous,
Cela fait un petit moment que je cherche pourquoi je n'arrive pas à binder la propriété ItemsSource de mon DataGridComboBoxColumn, sans trouver de solution à mon problème.
Le code de ma fenêtre wpf :
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
|
<Window x:Class="TestMVVM.View.FichePiloteView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:View="clr-namespace:TestMVVM.View"
Name="fichePiloteView"
Title="FichePiloteView"
Height="300" Width="737">
<DockPanel>
<Grid DockPanel.Dock="Left" Width="226">
<Label Content="Nom" Height="28" HorizontalAlignment="Left" Margin="12,22,0,0" Name="label1" VerticalAlignment="Top" />
<Label Content="Prénom" Height="28" HorizontalAlignment="Left" Margin="12,56,0,0" Name="label2" VerticalAlignment="Top" />
<Label Content="Club" Height="28" HorizontalAlignment="Left" Margin="12,90,0,0" Name="label3" VerticalAlignment="Top" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="81,24,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Text="{Binding Pilote.Nom}" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="81,58,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="{Binding Pilote.Prenom}" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="81,90,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding ListeClubs}" SelectedItem="{Binding Pilote.Club}" />
</Grid>
<Grid DockPanel.Dock="Right">
<Grid.RowDefinitions>
<RowDefinition Height="24*" />
<RowDefinition Height="237*" />
</Grid.RowDefinitions>
<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Stretch" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Path=Pilote.PiloteCategories}">
<DataGrid.Columns>
<DataGridTextColumn Header="N° machine" Binding="{Binding NumeroMachine}" />
<!-- C'est ici que j'ai un problème -->
<DataGridComboBoxColumn Header="Catégorie" ItemsSource="{Binding ElementName=fichePiloteView, Path=DataContext.ListeCategories}" SelectedItemBinding="{Binding Categorie.Nom}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</DockPanel>
</Window> |
la fenêtre est créée de la manière suivante :
Code:
1 2 3 4
|
var fichePilote = new FichePiloteView();
fichePilote.DataContext = new FichePiloteViewModel(pilote);
fichePilote.ShowDialog(); |
Les propriétés de la classe FichePiloteViewModel sont les suivantes :
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
|
class FichePiloteViewModel
{
[...]
private readonly IEnumerable<ClubViewModel> listeClubs;
private readonly IEnumerable<Categorie> listeCategories;
private Pilote modifiedPilote;
/// <summary>
/// Obtenir la liste des clubs.
/// </summary>
public IEnumerable<ClubViewModel> ListeClubs
{
get
{
return listeClubs;
}
}
/// <summary>
/// Obtenir la liste des catégories.
/// </summary>
public IEnumerable<Categorie> ListeCategories
{
get
{
return listeCategories;
}
}
/// <summary>
/// Obtenir le pilote en cours de modification.
/// </summary>
public Pilote Pilote
{
get
{
return modifiedPilote;
}
}
[...]
} |
J'obtiens l'erreur suivante :
Citation:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.ListeCategories; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=17600675); target property is 'ItemsSource' (type 'IEnumerable')
J'espère avoir été suffisamment clair.
Merci d'avance pour vos réponses.
EDIT :
J'ai réussis à résoudre mon problème de la manière suivante :
j'ai remplacé la portion de code :
Code:
1 2
|
<DataGridComboBoxColumn Header="Catégorie" ItemsSource="{Binding ElementName=fichePiloteView, Path=DataContext.ListeCategories}" SelectedItemBinding="{Binding Categorie.Nom}" /> |
par :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
<DataGridTemplateColumn Header="Catégorie">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Categorie.Nom}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ListeCategories}" SelectedItem="{Binding Categorie}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn> |
A noter que la solution suivante ne fonctionne pas :
Code:
1 2
|
<DataGridComboBoxColumn Header="Catégorie" ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.ListeCategories}" SelectedItemBinding="{Binding Categorie}" /> |
Si quelqu'un à une explication je suis preneur.