如何在MVVMLight RelayCommand方案中更新UI?

NoobDeveloper

这是一个简单的屏幕,textblock最初的屏幕为“”,button称为“设置文本”,用于将文本设置为textblockanother button称为“清除文本”,其始终清除textblock这就是XAML的样子。

<StackPanel>
    <TextBlock Text="{Binding DisplayText, Mode=TwoWay}"></TextBlock>
    <Button Content="Set Text" Command="{Binding SetTextCommand}"></Button>
    <Button Content="Clear Text" Command="{Binding CancelCommand}" 
                                 IsEnabled="{Binding CanCancel, Mode=TwoWay}"/>
</StackPanel>

这是我的ViewModel代码。

public class Page1VM : ViewModelBase
    {

        public RelayCommand SetTextCommand { get; private set; }
        public RelayCommand CancelCommand { get; private set; }

        public Page1VM()
        {
            SetTextCommand = new RelayCommand(HandleSetText, CanExecute);
            CancelCommand = new RelayCommand(HandleClearButtonClick, CanExecuteCancel);
        }

        private void HandleSetText(string number)
        {
            DisplayText = number;
        }

        private string _displayText="";
        public string DisplayText
        {
            get { return _displayText; }
            set
            {
                _displayText = value;
                RaisePropertyChanged("DisplayText");
                RaisePropertyChanged("CanCancel");
            }
        }

        private bool _canCancel;
        public bool CanCancel
        {
            get
            {
                if (DisplayText == "")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            set
            {
                _canCancel = value;
                RaisePropertyChanged("CanCancel");
            }
        }

        private bool CanExecute()
        {
            return true;
        }
        private bool CanExecuteCancel()
        {
            if (DisplayText == "")
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private void HandleClearButtonClick()
        {
            DisplayText = "";
        }
        private void HandleSetText()
        {
            DisplayText = "Hello";
        }
    }

问题:加载页面后,“清除文本”按钮被禁用,这是预期的,并且可以按预期工作。

当我单击“设置文本”时,我通过将文本值设置为named属性DisplayText并调用RaisePropertyChanged("CanCancel");将文本设置为文本块,但是即使在未启用“清除文本”按钮之后,我将其设置为文本块背后的原因可能是什么?我的文本块显示了文本值,但“明文”按钮仍未启用。

马克

据我所知,您的示例中有些混乱:您基本上不使用“ RelayCommand”的内置“ CanExecute”机制,而是在自己定义CanExecute“ RealyCommand 方法的同时重新构建它'。“ CanExecute”的想法是自动取消无法执行命令的控件,因此您无需手动执行。在“ CanExecute”方法中返回“ true”实际上没有任何意义,因为您不一定需要在RelayCommand中有一个CanExecute委托(... = new RelayCommand(ExecuteCommand);很好)。您的方案不起作用,因为您没有在“ CancelCommand”上调用“ RaisCanExecuteChanged()”。

尝试以下实现,我已经删除了多余的部分,并插入了缺少的“ RaiseCanExecuteChanged()”。请参阅注释以获取解释:

<StackPanel>
    <TextBlock Text="{Binding DisplayText, Mode=TwoWay}"></TextBlock>
    <Button Content="Set Text" Command="{Binding SetTextCommand}"></Button>
    <Button Content="Clear Text" Command="{Binding CancelCommand}" />
</StackPanel>

并使用此简化的ViewModel:

public class Page1VM : ViewModelBase
{
    public RelayCommand SetTextCommand { get; private set; }

    public RelayCommand CancelCommand { get; private set; }

    public Page1VM()
    {
        SetTextCommand = new RelayCommand(ExecuteSetText);

        CancelCommand = new RelayCommand(ExecuteCancel, CanExecuteCancel);
    }

    private string _displayText="";

    public string DisplayText
    {
        get { return _displayText; }
        set
        {
            _displayText = value;
            RaisePropertyChanged("DisplayText");
            RaisePropertyChanged("CanCancel");
            // Raise the CanExecuteChanged event of CancelCommand
            // This makes the UI reevaluate the CanExecuteCancel
            // Set a breakpoint in CanExecuteCancel method to make
            // sure it is hit when changing the text
            CancelCommand.RaiseCanExecuteChanged();                
        }
    }

    private bool CanExecuteCancel()
    {
        // You can simplify the statement like this:
        return DisplayText != "";
    }

    private void ExecuteCancel()
    {
        DisplayText = "";
    }

    private void ExecuteSetText()
    {
        DisplayText = "Hello";
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在大批量模型更新方案中处理无响应的UI

来自分类Dev

Mvvmlight RelayCommand中canExecute的奇怪行为

来自分类Dev

将按钮绑定到 Wpf MVVMLight 中的 RelayCommand

来自分类Dev

如何在MVVM中强制更新UI?

来自分类Dev

TextBox在MvvmLight中不更新

来自分类Dev

如何在WPF中手动触发RelayCommand?

来自分类Dev

如何在不同项目中使用MvvmLight SimpleIoc

来自分类Dev

如何在不同项目中使用MvvmLight SimpleIoc

来自分类Dev

如何在MvvmLight的IoC中注册多个接口实现?

来自分类Dev

如何在SearchDelegate的buildResults中传递更新UI值?

来自分类Dev

如何在UITabbarController中更新默认标志UI外观?

来自分类Dev

如何在android中更新我的UI值?

来自分类Dev

如何在Visual Studio 2019中更新.sln(解决方案)文件

来自分类Dev

如何将ListView DataTemplate中的按钮绑定到ViewModel中的命令?(MVVMLight)

来自分类Dev

MVVMLight中的IoC容器-如何将具体实现传递给指定的元素?

来自分类Dev

在我的方案中如何使用MVVM动态更新数据网格

来自分类Dev

如何使MvvmLight命令异步?

来自分类Dev

如何在Xcode中更改方案的名称?

来自分类Dev

如何在方案中从图中删除节点?

来自分类Dev

如何在.NET中测试并发方案?

来自分类Dev

如何在方案中执行JavaScript的>>>?

来自分类Dev

如何在Corrplot中更改配色方案

来自分类Dev

如何在Laravel中创建种子方案

来自分类Dev

如何在方案中调用函数?

来自分类Dev

如何在SqlDataReader中处理多行方案?

来自分类Dev

如何在Jupyter中更改配色方案?

来自分类Dev

如何在.NET中测试并发方案?

来自分类Dev

你如何在方案中求和

来自分类Dev

如何在PyCharm中添加配色方案?

Related 相关文章

热门标签

归档