I have been attempting to successfully bind a Button's isEnabled property to a Boolean variable. After updating the Boolean, the Button's enabled state does not change. I used the helpful documentation from Microsoft for this subject to form my code, found here . Upon searching StackOverflow, I came across this article a regarding similar problem. It did not assist me in fixing this bug.
public class BackState : INotifyPropertyChanged
{
public bool _isEnabled;
public bool isEnabled
{
get
{
return _isEnabled;
}
set
{
if (value != _isEnabled)
{
_isEnabled = value;
NotifyPropertyChanged("isEnabled");
Debug.WriteLine("NotifyPropertyChanged was called successfully");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(info));
}
}
Snippet of XAML code:
<Grid DataContext="{Binding BackObject}" HorizontalAlignment="Stretch" Height="250" VerticalAlignment="Top">
<Button x:FieldModifier="public" IsEnabled="{Binding isEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Click="Back_Click" x:Name="Back" Foreground="DarkGray" Content="" FontFamily="Segoe MDL2 Assets" Background="#33FFFFFF" FontSize="20" Margin="40,70,0,133" />
</Grid>
My Boolean Change Function
public static void ChangeState(bool b)
{
BackState bs = new BackState();
if(b == true)
{
bs.isEnabled = true;
Debug.WriteLine("Enabled Property");
}else{
bs.isEnabled = false;
Debug.WriteLine("Disabled Property");
}
}
And here is the BackObject definition
<local:BackState x:Key="BackObject"/>
In the GUI, the button constantly remains enabled. (Regardless of if ChangeState function is called) It should change disable/enable according to the called function.
In your ChangeState
method you are creating a new BackState object, whereas this should be a property on the BackObject
that you are setting as the DataContext
. Instantiate a BackState
that is a property on the BackObject
, and bind to the isEnabled
property of that object so that the xaml is told to listen for changes to that property.
<Grid DataContext="{Binding BackObject}" HorizontalAlignment="Stretch" Height="250" VerticalAlignment="Top">
<Button x:FieldModifier="public" IsEnabled="{Binding yourPropertyWhcihIsBackState.isEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Click="Back_Click" x:Name="Back" Foreground="DarkGray" Content="" FontFamily="Segoe MDL2 Assets" Background="#33FFFFFF" FontSize="20" Margin="40,70,0,133" />
</Grid>
and then in the method:
public static void ChangeState(bool b)
{
if(b == true)
{
yourPropertyWhcihIsBackState.isEnabled = true;
Debug.WriteLine("Enabled Property");
}else{
yourPropertyWhcihIsBackState.isEnabled = false;
Debug.WriteLine("Disabled Property");
}
}
But this won't work if you are ultimately making changes on an instance of an object to which the xaml is not bound. You seem to be creating a new instance of BackObject
in your xaml, but it doesn't look like you have any way of accessing this object in your code. Consider setting up a ViewModel
for accomplishing this. Take a look at mvvm light , it's a great mvvm (Model View ViewModel) framework.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.