使用 DataGrids(用户控件)SelectedItem / Row WPF XAML 以编程方式检查复选框(用户控件)
我有两个自定义控件。首先,我有一个复选框自定义控件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所知,复选框是托管在数据网格行上的用户控件的后代。
因此,您必须使用
myCheckboxControl
中的中介属性来保存CheckBox.IsChecked
来解决这 2 级边界。您可以在myCheckboxControl
中引入一个新的依赖属性,例如IsCheckBoxChecked
并在进一步讨论中使用它。我正在使用另一个名为
Tag
的属性,它是人们可能想要针对框架元素添加的任何额外信息的占位符。因此,当您以编程方式选择数据网格行时,将选中该行上的相应复选框。此外,当您选中该复选框时,该行将被选中,反之亦然。
现在,如果您不希望在选中复选框时进行选择,则必须在行项级别引入基于
INotifyPropertyChanged
的可通知属性。例如,如果您要绑定列表将员工添加到数据网格,那么每个员工类必须有一个名为“IsSelected”的可设置属性。此类必须实现 INotifyPropertyChanged 接口,并且应从
IsSelected
属性的 setter 引发属性更改通知。在这种情况下,绑定更改为这样...
让我知道这是否有帮助。
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 holdCheckBox.IsChecked
. You can introduce a new dependency property inmyCheckboxControl
sayIsCheckBoxChecked
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.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 thatIsSelected
property.In such case the binding is changed to this...
Let me know if this helps.