-1

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.

enter image description here

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. enter image description here enter image description here

Please help me! Thanks!

Ricky Chen
  • 55
  • 6
  • What gets into your `var parentElement` when you drop the casting to `ContentPresenter`? If wrong casting won't throw an exception, best case scenario is that you get null. – Mike Mar 26 '20 at 12:17
  • Please provide a concrete example. How and where is `CheckBox_Click` hooked up? – mm8 Mar 26 '20 at 12:51
  • @mm8 CheckBox_Click is CheckBox event, so how numbers in CheckBox are decided by ObservableCollection. – Ricky Chen Mar 27 '20 at 00:50
  • @Mike I have added some pictures. I get lots of ContentPresenter's property, and datacontext is CheckBox. – Ricky Chen Mar 27 '20 at 01:01
  • I temporarily solve this problem by adding PreviewMouseUp event at ItemsControl(B), and get parent successfully. But I think it will be a better answer than this. – Ricky Chen Mar 27 '20 at 08:26

1 Answers1

1

@RickyChen - I see your problem. AuthorityCheckBox binds to your visual checkboxes. When you click on a checkbox, there isn't any link back to the AuthorityCheckBox.

What you can do is abuse the Tag property of your checkbox and place the AuthorityCheckBox reference there.

Adjust your your AuthorityCheckBox class so it contains public Account Parent that is assigned in the constructor. This way you can easily get the AuthorityCheckBox parent:

public class AuthorityCheckBox
{
    public string Content { get; set; }
    public bool IsChecked { get; set; }
    public bool IsEnabled { get; set; }
    public Account Parent { get; private set; }

    public AuthorirtyCheckBox(Account parent)
    {
         this.Parent = parent;
    }
}

The event handler then looks like this:

private void CheckBox_Click(object sender, RoutedEventArgs e)
{
    AuthorityCheckBox acb = (sender as FrameworkElement).Tag as AuthorityCheckBox;
    Account parent = acb.Parent;           
}
Mike
  • 1,225
  • 10
  • 21
  • Mike, Thanks for replying me. I adjusted my AuthorityCheckBox class and CheckBox_Click, but (sender as FrameworkElement).Tag is null, :( – Ricky Chen Mar 30 '20 at 01:34
  • Another one, I use this."CheckBox checkBox = sender as CheckBox;", "AuthorityCheckBox acb = checkBox.DataContext as AuthorityCheckBox;", "Account account = acb.Parent;", it worked!! Thanks for giving me inspiration! – Ricky Chen Mar 30 '20 at 01:43
  • 1
    @RickyChen, glad you could make it work for you. Usually `Tag` property is empty unless you assign it some value. For you it was null since you didn't fill it in. – Mike Mar 30 '20 at 07:39
  • OK, I appreciate your kind assistance and find a solution to solve this problem. Very Nice! – Ricky Chen Mar 31 '20 at 06:07