简体   繁体   中英

Bind DependencyProperty to Property in another class

In my MainWindow.xaml, I have added my UserControl to the Window. There is also a button that toggles a Boolean value call IsVisible from True/False.

In my MainUserControl.xaml, I have my datagrid(not MS grid) where its tempate points to a Control Template set in the App.xaml. This is where my IsVisible property exists.

In my App.xaml, I have a ControlTemplate which has my datagrid. If I want to bind the Visible property of my Column to the IsVisible property in the MainUserControl class.

I'm having trouble binding the property. I've tried using TemplateBinding, but that can't work since the the TemplatedParent is not the UserControl. I've tried using RelativeSource and setting the Ancestor, but that didn't work either. Does anyone have any advice to help me out?

EDIT: this is the code in my ControlTemplate

<Column FieldName="Column1" 
             Title="Column1" 
             Visible="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}, AncestorLevel=2}, Path=IsVisible}"  />

The problem is the fact you are setting AncestorLevel=2 , try removing that and it should work for you.

AncestorLevel=2 would only allow this to work if you had to UserControl1 s, one inside the other as it says bind to the second instance of local:UserControl1 encountered when traversing up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM