How to set binding in xaml to my variable

I have an object in 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 the bindings to the Button.IsEnabled property. That is, when the user is not registered yet, some buttons are disabled. How can I do that? I tried in xaml:

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

but this work dos't and OutputWindow says: System.Windows.Data Error: 39: BindingExpression path error: IsUserLogin property not found on 'object'. I am also trying to set the Button.DataContext property for LoginState in code, but has a XamlParseException. I also read this [ WPF - binding in XAML to the object created in the code behind , but still cannot set the bindings.

[1]: WPF - binding in XAML to an object created in the code . Please, help!

+3
1

XamlException, (XAML) -Element "LoginState".

DataContext . .

public void SetDataContextOfButton() {
  UserLogin login = new UserLogin();
  button.DataContext = login; //Assumes that you name your Button in XAML with x:Name="button"
}
+1

Source: https://habr.com/ru/post/1737729/


All Articles