简体   繁体   中英

Bind to Property in Another Class

I've read up on binding to other classes but the syntax is tripping me up.

I have properties in my MainWindow.xaml.cs and I'd like to reference them SecondWindow.xaml .

I tried {Binding Source={x:Static local:MainWindow.Test}} but it complains about Key being null.

Update

Following the suggestion, I set the SecondWindow context to my MainWindow as well as tried setting the ParentHandle .

When the application runs, I get a bunch of runtime errors like this.

System.Windows.Data Error: 40 : BindingExpression path error: 'LabelColor' property not found on 'object' ''SecondWindow' (Name='secondWindow')'. BindingExpression:Path=LabelColor;

I think you can do something like this:

// I suppose MainWindow will new up an instance during some event.
var window = new SecondWindow();
window.DataContext = this; // Set to MainWindow
window.Show();

Then access it like this:

{Binding Test}

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