【问题标题】:Programmatically check a checkbox (usercontrol) withing a DataGrids (usercontrol) SelectedItem / Row WPF XAML以编程方式检查 DataGrid(用户控件)SelectedItem / Row WPF XAML 中的复选框(用户控件)
【发布时间】:2011-10-20 21:39:46
【问题描述】:

我有两个自定义控件。首先我有一个复选框自定义控件,myCheckboxControl,(下面是简化的 xaml)

<UserControl x:Class="UserControls.myCheckboxControl"><Grid>
        <CheckBox x:Name="chkboxList" HorizontalAlignment="Center" Checked="chkboxList_Checked">
</Grid></UserControl>

我还有一个自定义 DataGrid 控件(以下简化的 xaml),其中包含 DataTemplate 中的复选框控件

<UserControlx:Class="UserControls.myDataGridControl"><DataGrid x:Name="dgMyGrid>
<DataGrid.Columns>
          <DataGridTemplateColumn x:Name="tempCol" Header="Checkbox(L)">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <localControls:myCheckboxControl x:Name="controlList"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

然后我的主窗口中有 DataGrid (myDataGridControl)。

我的问题是我在 MainWindow 上有一个按钮。单击该按钮时,我还需要它来检查 myCheckboxControl 中的复选框。我可以获得数据网格的 SelectedItem,但不确定如何让我的 2 级深度复选框被选中。

提前致谢。

【问题讨论】:

    标签: wpf datagrid checkbox custom-controls


    【解决方案1】:

    正如您已经知道,复选框是托管在数据网格行上的用户控件的后代。

    因此,您必须通过使用myCheckboxControl 的中介属性来保持CheckBox.IsChecked 来解决这两个级别的界限。您可以在myCheckboxControl 中引入一个新的依赖属性,比如IsCheckBoxChecked,以便进一步讨论。

    我正在使用另一个名为 Tag 的属性,它是一个占位符,用于存储可能要针对框架元素添加的任何额外信息。

        <UserControl x:Class="UserControls.myCheckboxControl">
            <Grid>
                <CheckBox x:Name="chkboxList"
                          HorizontalAlignment="Center"
                          IsChecked="{Binding
                                        Tag,
                                        RelativeSource={RelativeSource
                                            AncestorType={x:Type UserControl}}
                                        Mode=TwoWay}">
           </Grid>
       </UserControl>
    
    
       <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <localControls:myCheckboxControl
                           Tag="{Binding
                                    IsSelected,
                                    Mode=TwoWay,
                                    RelativeSource={RelativeSource
                                       AncestorType={x:Type DataGridRow}}}"
                           x:Name="controlList"/>
            </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>  
    

    因此,当您以编程方式选择数据网格行时,该行上的相应复选框将被选中。此外,当您选中复选框时,该行将被选中,反之亦然。

    现在,如果您不想在选中复选框时进行选择,则必须在行项目级别引入基于 INotifyPropertyChanged 的通知属性。

    例如如果要将员工列表绑定到数据网格,则每个员工类都必须有一个名为“IsSelected”的可设置属性。此类必须实现INotifyPropertyChanged 接口,并且应该从IsSelected 属性的setter 发出属性更改通知。

    在这种情况下,绑定更改为 this...

                <localControls:myCheckboxControl
                           Tag="{Binding
                                    IsSelected,
                                    Mode=TwoWay}"
                           x:Name="controlList"/>
    

    如果这有帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多