使用 DataGrids(用户控件)SelectedItem / Row WPF XAML 以编程方式检查复选框(用户控件)

发布于 2024-12-11 06:06:58 字数 1154 浏览 0 评论 0原文

我有两个自定义控件。首先,我有一个复选框自定义控件 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)。

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

提前致谢。

I have TWO custom controls. First I have an checkbox custom control, myCheckboxControl, (simplied xaml below)

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

I also have a a custom DataGrid control (simplified xaml below) that contains the checkbox control in a 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>

Then I have the DataGrid (myDataGridControl) in my MainWindow.

The question I have is that I have a Button on the MainWindow. When that Button is Clicked, I need it to also check the checkbox within myCheckboxControl. I can get the SelectedItem of the datagrid, but just not sure how to get my 2 level deep checkbox to get checked.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-12-18 06:06:58

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

因此,您必须使用 myCheckboxControl 中的中介属性来保存 CheckBox.IsChecked 来解决这 2 级边界。您可以在 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 引发属性更改通知。

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

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

让我知道这是否有帮助。

As you have already known that the checkbox is a descendent of a user control which is hosted on a datagrid row.

So you will have to resolve these 2 level boundaries by using a mediator property at myCheckboxControl to hold CheckBox.IsChecked. You can introduce a new dependency property in myCheckboxControl say IsCheckBoxChecked an use that in further discussion.

I am using another property called Tag which is a placeholder for any extra information one may want to add against a framework element.

    <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>  

Thus when you programmatically select datagrid row(s) then corresponding checkbox on that row will get checked. Also when you check the checkbox the row will get selected and vice versa.

Now if you dont want selection to take place upon checking the checkbox, you will have to introduce a INotifyPropertyChanged based notifiable property at row item level.

E.g. if you are binding a list of employees to the datagrid then each employee class must have a settable property called "IsSelected". This class must implement INotifyPropertyChanged interface and should raise a property changed notification from setter that IsSelected property.

In such case the binding is changed to this...

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

Let me know if this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文