开发者

How to access DataGridCell from its child control

I have a DataGrid with template column, containing a checkbox:

<DataGridTemplateColumn Header="Foreign key">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox HorizontalAlign开发者_Python百科ment="Center" 
                VerticalAlignment="Center" 
                IsChecked="{Binding ForeignKey,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                PreviewMouseDown="CheckBox_PreviewMouseDown" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

How can I access a DataGridCell containing this checkbox from inside the handler (CheckBox_PreviewMouseDown), having only CheckBox as sender:

private void CheckBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    CheckBox checkBox = sender as CheckBox;
    ???
}


If you need to access the UI control, iterate through the visual tree using a helper. Or, if you just need the databinding, use the Tag property of the CheckBox.

<CheckBox HorizontalAlignment="Center"
          VerticalAlignment="Center"
          IsChecked="{Binding ForeignKey,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          PreviewMouseDown="CheckBox_PreviewMouseDown"
          Tag="{Binding}" />

Then you can access it in your code, and typecast it to the type of the bound item(s).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜