views:

34

answers:

1

Hello. I have an object in the code:

public  class UserLogin
    {
        bool _IsUserLogin = false;

        public bool IsUserLogin
        {
            get { return _IsUserLogin; }
            set { _IsUserLogin = value; }
        }

        public UserLogin()
        {
            _IsUserLogin = false;
        }
    }
    ....
    public static UserLogin LoginState;
    .....
    LoginState = new UserLogin();

And I need to set bindings to Button.IsEnabled property. I.e. when user not login yet - some buttons are disabled. How can this been done? I have try in xaml:

<Button DataContext="LoginState" IsEnabled="{Binding Path=IsUserLogin}">

but, this dos't work and OutputWindow says: System.Windows.Data Error: 39 : BindingExpression path error: 'IsUserLogin' property not found on 'object'. I also have try to set Button.DataContext property to LoginState in the code, but have XamlParseException. I also read this one [http://stackoverflow.com/questions/1829758/wpf-binding-in-xaml-to-an-object-created-in-the-code-behind][1] but still can't set bindings.

[1]: http://stackoverflow.com/questions/1829758/wpf-binding-in-xaml-to-an-object-created-in-the-code-behind. Please help!

+1  A: 

You get the XamlException cause the compiler doesn´t find an (XAML)-Element with the name "LoginState".

Set the DataContext of the button in procedural code. Then it should work.

public void SetDataContextOfButton() {
  UserLogin login = new UserLogin();
  button.DataContext = login; //Assumes that you name your Button in XAML with x:Name="button"
}
Jehof
I'am an idiot, I was try to set DataContext of the button before InitializeComponent();Thanks!
Victor