【问题标题】:C# WPF MVVM Light CanExecute not recognizedC# WPF MVVM Light CanExecute 无法识别
【发布时间】:2015-09-08 12:38:30
【问题描述】:

我现在正在学习 WPF 和 MVVM 并且遇到了一个小问题。

我正在使用 MVVM Light,我希望在验证后禁用/启用一些按钮,但它没有使用这些功能。

ViewModelMain:

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace EF_MVVM_Test
{
    public class ViewModelMain : ViewModelBase
    {
        public RelayCommand AddAuthorCommand { get; private set; }
        public RelayCommand DeleteAuthorCommand { get; private set; }
        public RelayCommand RefreshCommand { get; private set; }
        public RelayCommand SaveCommand { get; private set; }

        public ObservableCollection<Author> Authors { get; set; }
        private LibraryContext db;

        private Author _SelectedAuthor;
        public Author SelectedAuthor
        {
            get { return _SelectedAuthor; }
            set { Set("SelectedAuthor", ref _SelectedAuthor, value); }
        }

        private Author _NewAuthor;
        public Author NewAuthor
        {
            get { return _NewAuthor; }
            set { Set("NewAuthor", ref _NewAuthor, value); }
        }

        public ViewModelMain()
        {
            db = new LibraryContext();
            db.Author.Load();
            Authors = db.Author.Local;

            AddAuthorCommand = new RelayCommand(ExecuteAddAuthorCommand, CanExecuteAddAuthorCommand);
            DeleteAuthorCommand = new RelayCommand(ExecuteDeleteAuthorCommand, CanExecuteDeleteAuthorCommand);
            RefreshCommand = new RelayCommand(ExecuteRefreshListCommand);
            SaveCommand = new RelayCommand(ExecuteSaveCommand);

            NewAuthor = new Author();
        }

        private void ExecuteAddAuthorCommand()
        {
            db.Author.Add(_NewAuthor);
            NewAuthor = new Author();
        }
        private void ExecuteDeleteAuthorCommand()
        {
            db.Author.Remove(SelectedAuthor);
        }
        private void ExecuteRefreshListCommand()
        {
            db.Author.Load();
            Authors = db.Author.Local;
        }
        private void ExecuteSaveCommand()
        {
            db.SaveChanges();
        }

        private bool CanExecuteAddAuthorCommand()
        {
            return (!string.IsNullOrEmpty(NewAuthor.Name) && NewAuthor.Birthday != null);
        }
        private bool CanExecuteDeleteAuthorCommand()
        {
            return SelectedAuthor != null;
        }
    }
}

XAML:

<StackPanel>
        <DataGrid ItemsSource="{Binding Authors}" SelectedItem="{Binding SelectedAuthor}" AutoGenerateColumns="False" Height="200" Margin="5" CanUserAddRows="False" CanUserDeleteRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
                <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}"/>
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Name" Margin="5" VerticalAlignment="Center"/>
            <TextBox Height="25" Width="70" Margin="5" Text="{Binding NewAuthor.Name}"/>
            <TextBlock Text="Geburtstag" Margin="5" VerticalAlignment="Center"/>
            <DatePicker Height="25" Width="130" Margin="5" SelectedDate="{Binding NewAuthor.Birthday}"/>
            <Button Margin="5" Height="25" Width="80" Content="Hinzufügen" HorizontalAlignment="Left" Command="{Binding AddAuthorCommand}"/>
            <Button Margin="5" Height="25" Width="80" Content="Löschen" HorizontalAlignment="Left" Command="{Binding DeleteAuthorCommand}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button Width="120" Height="25" Content="Speichern" HorizontalAlignment="Left" Margin="5" Command="{Binding SaveCommand}"/>
            <!--Button Width="120" Height="25" Content="Aktualisieren" HorizontalAlignment="Left" Margin="5" Command="{Binding RefreshCommand}"/-->
        </StackPanel>
</StackPanel>

任何想法为什么按钮不使用 canexecute 功能?

【问题讨论】:

    标签: c# wpf xaml mvvm mvvm-light


    【解决方案1】:

    您的问题是您正在使用的命名空间:

    using GalaSoft.MvvmLight.Command;
    

    替换为:

    using GalaSoft.MvvmLight.CommandWpf;
    

    【讨论】:

    • 你无法想象这个答案为我节省了多少时间。非常感谢。
    【解决方案2】:

    WPF 的 CommandManager 在检测到 UI 更改时会调用 CanExecute?什么构成 UI 更改?添加和删​​除元素,一些绑定更新,视觉状态改变。基本上,只要 WPF 喜欢它。这是不可靠的。

    你怎么能绕过这个?当您需要刷新RelayCommand 时,请致电RelayCommand.RaiseCanExecuteChanged()。此方法引发一个事件,通知 WPF 它应该重新评估命令的状态。

    【讨论】:

    【解决方案3】:

    看来你几天前遇到了和我一样的问题。

    ICommand doesn't work

    ICommand.CanExecute 没有调用,因为您当然不会说按钮:“当我在TextBox 中插入某些内容时,按钮单击的能力可能会改变”。因此,如果文本框文本发生更改,您的按钮不会意识到他可以启用。您必须明确告诉按钮它应该在文本更改时检查ICommand.CanExecute,方法是在文本框tex 发生更改时引发ICommand.CanExecuteChanged 事件。

    【讨论】:

      【解决方案4】:

      我认为,不幸的是,您必须在分配 SelectedAuthor 并拦截 NewAuthor 对象的属性更改后调用 RelayCommand 对象的 RaiseCanExecuteChanged 方法。

      【讨论】:

        猜你喜欢
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多