DataGridCell は、コントロールの VisualTree に表示されません。
グリッド内のスタック パネルに Rectangle と Label を含む DataGridTemplateColumn があります。
<t:DataGridTemplateColumn>
<t:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid FocusManager.FocusedElement="{Binding ElementName=swatch}">
<StackPanel Orientation="Horizontal">
<Rectangle Name="swatch" PreviewMouseLeftButtonDown="swatch_PreviewMouseLeftButtonDown" />
<Label/>
</StackPanel>
</Grid>
</DataTemplate>
</t:DataGridTemplateColumn.CellTemplate>
</t:DataGridTemplateColumn>
親要素がnullになるPreviewMouseLeftButtonDown
まで、イベントがビジュアルツリーを上向きに反復するようにしたかったのです。DataGridCell
Grid
private void swatch_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = null;
while (cell == null)
{
cell = sender as DataGridCell;
sender = ((FrameworkElement)sender).Parent;
}
MethodForCell(sender);
}
以下のリンクを読むと、DataGrid のビジュアル ツリーで、UIControls が の content プロパティとして設定されているようDataGridCell
です。では、Rectangle から DataGridCell を取得するにはどうすればよいでしょうか。
http://blogs.msdn.com/b/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx