CanExecute-Result 更改后控件不刷新

发布于 2024-07-15 03:26:51 字数 812 浏览 5 评论 0原文

在我的窗口中,我有用于加载和保存方法的按钮。 我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-07-22 03:26:51

好吧,我自己发现了。

canSaveXML = true;
CommandManager.InvalidateRequerySuggested();

是后台工作者内部的代码。 不好。 您必须告诉窗口的调度程序调用 CommandManager.InvalidateRequerySuggested();

Ok, I found out myself.

canSaveXML = true;
CommandManager.InvalidateRequerySuggested();

was code inside a background worker. Not good. You have to tell the dispatcher of the window to invoke CommandManager.InvalidateRequerySuggested();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文