I have a ItemsControl(A). ItemSourece is a Class "Account".It contains some controls and another ItemsControl(B).
ItemsControl(B) includes some CheckBox. It's ItemSourece is ObservableCollection included in Class "Account". CheckBox's Content is binding to Content. CheckBox's IsChecked is bindind to IsChecked.
Now when I click CheckBox, I want to get ID and User in Class "Account", but I don't know hot to get them. I already try to use
private void CheckBox_Click(object sender, RoutedEventArgs e)
{
CheckBox checkBox = sender as CheckBox;
var parentElement = (ContentPresenter)VisualTreeHelper.GetParent(checkBox);
}
but it still can't get parent. Althouth runtime can show parentElement.VisualParent, but actually it is not work.
Please help me! Thanks!
@RickyChen - I see your problem.
AuthorityCheckBox
binds to your visual checkboxes. When you click on a checkbox, there isn't any link back to theAuthorityCheckBox
.What you can do is abuse the
Tag
property of your checkbox and place theAuthorityCheckBox
reference there.Adjust your your
AuthorityCheckBox
class so it containspublic Account Parent
that is assigned in the constructor. This way you can easily get theAuthorityCheckBox
parent:The event handler then looks like this: