[英]How to get the checkbox column sender's datagrid parent
Hi I have a datagrid (xaml code below):嗨,我有一个数据网格(下面的 xaml 代码):
<DataGrid.Columns>
<DataGridTextColumn Header="FLEET" IsReadOnly="True" Width="1*" Binding="{Binding FLEET, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTemplateColumn Header="SELECTED?" Width="1*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Viewbox Margin="-0.2" Height="18.5">
<CheckBox IsThreeState="True" IsChecked="{Binding Path=isSelected, UpdateSourceTrigger=PropertyChanged}" Click="FSC_CheckBox_Click"/>
</Viewbox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
The click events:点击事件:
private void FSC_CheckBox_Click(object sender, RoutedEventArgs e)
{
......
}
Is there a way I can get the the parent datagrid object from the click sender?有没有办法从点击发送者那里获取父数据网格对象? I've tried this link Get containing row from a CheckBox inside of a DataGrid , but it didn't work because of the viewbox and datatemplate(I guess).我已经尝试过这个链接Get contains row from a CheckBox inside a DataGrid ,但由于视图框和数据模板(我猜),它不起作用。 Any suggestions is appreciated.任何建议表示赞赏。 Thank you.谢谢。
The click event handler will get the sender, which you can cast to checkbox.单击事件处理程序将获取发件人,您可以将其转换为复选框。
I reckon the simplest approach is to abuse the Tag property on your checkbox.我认为最简单的方法是滥用复选框上的 Tag 属性。
That can be any old object you fancy.那可以是您喜欢的任何旧对象。
You can use a relativesource binding for that.您可以为此使用相对源绑定。
Something like:类似的东西:
<CheckBox Tag="{Binding RelativeSource={Relativesource AncestorType=DataGrid}}"
Grab it off there, roughly:把它拿下来,大致:
private void FSC_CheckBox_Click(object sender, RoutedEventArgs e)
{
var cb=(CheckBox)sender;
var parentDataGrid = (DataGrid)cb.Tag;
}
This is air code, I don't have a datagrid with a checkbox and a click handler to try it out with easily so there may be some typo lurking.这是空气代码,我没有带有复选框和单击处理程序的数据网格可以轻松试用,因此可能存在一些错字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.