How to set binding in xaml to my own variable
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"
}
精彩评论