简体   繁体   English

如何获取复选框列发件人的数据网格父级

[英]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.

相关问题 如何获取DataGridTextColumn发送者的datagrid父级 - How to get the DataGridTextColumn sender's datagrid parent 当我单击wpf中不属于datagrid的复选框时,如何在datagrid中选中所有列标题复选框? - How to get all column header checkbox selected in datagrid when I click on a checkbox which is not a part of datagrid in wpf? 如何选择WPF DataGrid中DataGrid Header CheckBox上列的所有CheckBox - How to Select All CheckBox of a Column on DataGrid Header CheckBox in WPF DataGrid 如何通过WPF DataGrid中的DataGrid Header CheckBox选择列的所有CheckBox - How to Select All CheckBox of a Column by DataGrid Header CheckBox in WPF DataGrid 如何获得 <checkbox> 内部状态 <datagridtemplatecolumn> 在datagrid中任何行的复选框事件上 - How to get <checkbox> state inside <datagridtemplatecolumn> on any row's checkbox event in datagrid 如何从datagrid模板列访问复选框? - How to access checkbox from datagrid template column? 如何将Datagrid列的可见性绑定到WPF中的复选框 - How to bind the visibility of a Datagrid Column to a Checkbox in WPF C#SQL如何将特定列的数据获取到DataGrid - C# sql How to get specific column's data to datagrid 我如何在后面的代码中获取WPF DataGrid的Row CheckBox的Checked事件 - how can I get the WPF DataGrid's Row CheckBox's Checked Event in code behind 将复选框列添加到DataGrid - Add Checkbox Column to a DataGrid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM