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.
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
myCheckboxControlto holdCheckBox.IsChecked. You can introduce a new dependency property inmyCheckboxControlsayIsCheckBoxCheckedan use that in further discussion.I am using another property called
Tagwhich 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
INotifyPropertyChangedbased 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
INotifyPropertyChangedinterface and should raise a property changed notification from setter thatIsSelectedproperty.In such case the binding is changed to this…
Let me know if this helps.