jeudi 26 mars 2020

(WPF) How can I find ancestor about two ItemsControl in code behind?

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.

Please help me! Thanks!




Aucun commentaire:

Enregistrer un commentaire