开发者

How to set binding in xaml to my own variable

开发者 https://www.devze.com 2022-12-23 06:25 出处:网络
I have an object in the code: publicclass UserLogin { bool _IsUserLogin = false; public bool IsUserLogin {

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.Da开发者_JAVA百科taContext property to LoginState in the code, but have XamlParseException. I also read this one [WPF - Binding in XAML to an object created in the code behind but still can't set bindings.

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


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"
}
0

精彩评论

暂无评论...
验证码 换一张
取 消