[英]Binding of Button isEnabled Property to a Boolean Does Not Change When INotifyPropertyChanged Fires
I have been attempting to successfully bind a Button's isEnabled property to a Boolean variable. 我一直试图将Button的isEnabled属性成功绑定到布尔变量。 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 . 我使用了Microsoft提供的有关此主题的有用文档来构成我的代码,请参见此处 。 Upon searching StackOverflow, I came across this article a regarding similar problem. 在搜索StackOverflow时,我遇到了有关类似问题的这篇文章。 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: XAML代码段:
<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 这是BackObject的定义
<local:BackState x:Key="BackObject"/>
In the GUI, the button constantly remains enabled. 在GUI中,该按钮始终保持启用状态。 (Regardless of if ChangeState function is called) It should change disable/enable according to the called function. (无论是否调用ChangeState函数)都应根据调用的函数来更改禁用/启用状态。
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
. 在你ChangeState
的方法是创建一个新的BackState对象,而这应该是在一个属性BackObject
您正在设置的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. 实例化一个BackState
是在一个属性BackObject
,并绑定到isEnabled
使得XAML被告知要侦听更改该属性,对象的属性。
<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. 但是,如果最终要对未绑定xaml的对象的实例进行更改,则此方法将无效。 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. 您似乎在xaml中创建了BackObject
的新实例,但是您似乎无法以任何方式在代码中访问此对象。 Consider setting up a ViewModel
for accomplishing this. 考虑设置一个ViewModel
来完成此任务。 Take a look at mvvm light , it's a great mvvm (Model View ViewModel) framework. 看一下mvvm light ,它是一个很棒的mvvm(模型视图ViewModel)框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.