Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I bind a WPF control to a field's property?

Because I needed to split some functionality between classes, I've arrived at the following situation

xaml code

<CheckBox IsChecked="{Binding MyObjectField.MyBoolean}"  />

view model

...
public MyInternalObject MyObjectField;
...

MyObject class

public class MyInternalObject {
    ...
    public bool MyBoolean { get; set; }
    ...
}

It does not work unless I replicate the MyBoolean property in the View Model class.

public bool MyBoolean 
{ 
    get { return MyInternalObject.MyBoolean; }
    set { MyInternalObject.MyBoolean=value; }
}

Does anyone have an idea?

like image 523
Xavi Ivars Avatar asked Jul 24 '12 16:07

Xavi Ivars


2 Answers

You can't yet (in WPF Version 4.5 you can bind to a static property). But you can create your property in App.xaml.cs

public partial class App : Application
{
    public bool MyBoolean { get; set; }
}

and bind from everywhere.

<CheckBox IsChecked="{Binding MyBoolean, Source={x:Static Application.Current}}">
like image 106
LPL Avatar answered Oct 17 '22 12:10

LPL


No you cant . Because binding system uses Reflection to find the

Property in DataContext(i.e your VM)

It does not look for fields . I hope this will help.

like image 37
yo chauhan Avatar answered Oct 17 '22 11:10

yo chauhan