CanExecute-Result 更改后控件不刷新
在我的窗口中,我有用于加载和保存方法的按钮。 我使用 CommandBinding,并且保存按钮具有 CanExecute 属性,以防止用户在加载数据之前保存数据。
CanExecute-Methode 连接到一个名为“canSaveXML”的简单布尔值,
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (canSaveXML == false)
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
e.Handled = true;
}
我的目的是在加载数据后设置 canSaveXML = true,但在值更改后控件不会刷新。 我做了一些阅读,发现我必须调用 CommandManager.InvalidateRequerySuggested。 我现在这样做,我的代码如下所示。
canSaveXML = true;
CommandManager.InvalidateRequerySuggested();
但控件(按钮)仍然不刷新。 我仍然被禁用,直到我触发 UI 上的任何内容或最小混合/最大化窗口。 完成此操作后,该按钮已启用。
这里有什么问题吗?
在 MSDN 示例中,CommandManager.InvalidateRequerySuggested 被调度计时器一次又一次地调用,但我拒绝相信这将是唯一的解决方案。
in my window I have buttons for load and save methods. I use CommandBinding and the save-button has a CanExecute property to keep the user from saving the data before it is loaded.
The CanExecute-Methode is connected to a simple bool value called "canSaveXML"
private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
if (canSaveXML == false)
{
e.CanExecute = false;
}
else
{
e.CanExecute = true;
}
e.Handled = true;
}
My intention is to set canSaveXML = true after the data was loaded but the control does not refresh after the value did change. I did some reading and found out I have to call CommandManager.InvalidateRequerySuggested. I do that now and my code looks like this.
canSaveXML = true;
CommandManager.InvalidateRequerySuggested();
But the control (button) still does not refresh. I still is disabled untill I trigger anything on the UI or minimixe/maximize the window. After I did that the button is enabled.
What is wrong here?
In a MSDN sample CommandManager.InvalidateRequerySuggested is called with a dispatchertimer again and again but I refuse to believe that would be the only solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己发现了。
是后台工作者内部的代码。 不好。 您必须告诉窗口的调度程序调用 CommandManager.InvalidateRequerySuggested();
Ok, I found out myself.
was code inside a background worker. Not good. You have to tell the dispatcher of the window to invoke CommandManager.InvalidateRequerySuggested();