Etes-vous sûr que tout est correctement branché? Les travaux suivants pour moi:
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" MouseDown="Label_MouseDown">
Label 0, 0
</Label>
<Label Grid.Row="0" Grid.Column="1" MouseDown="Label_MouseDown">
Label 0, 1
</Label>
<Label Grid.Row="0" Grid.Column="2" MouseDown="Label_MouseDown">
Label 0, 2
</Label>
<Label Grid.Row="1" Grid.Column="0" MouseDown="Label_MouseDown">
Label 1, 0
</Label>
<Label Grid.Row="1" Grid.Column="1" MouseDown="Label_MouseDown">
Label 1, 1
</Label>
<Label Grid.Row="1" Grid.Column="2" MouseDown="Label_MouseDown">
Label 1, 2
</Label>
<Label Grid.Row="2" Grid.Column="0" MouseDown="Label_MouseDown">
Label 2, 0
</Label>
<Label Grid.Row="2" Grid.Column="1" MouseDown="Label_MouseDown">
Label 2, 1
</Label>
<Label Grid.Row="2" Grid.Column="2" MouseDown="Label_MouseDown">
Label 2, 2
</Label>
</Grid>
C#:
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
var label = e.Source as UIElement;
var row = Grid.GetRow(label);
var col = Grid.GetColumn(label);
MessageBox.Show(string.Format("{0},{1}", row, col));
}
Le MessageBox
contient la ligne correcte et de la colonne lorsque je clique sur l'une des étiquettes.
J'ai rencontré ce problème lors de l'ajout de contrôles par programmation à partir d'une classe C# non statique. –