简体   繁体   English

WPF mvvm 按钮(ICommand)可以执行不工作

[英]WPF mvvm Button (ICommand) CanExecute not Working

I have this xaml and this Code.我有这个 xaml 和这个代码。 My Problem is Confirm does not Change.我的问题是确认没有改变。 The CanExecute is nonly execut one times. CanExecute 只执行一次。

    <Label Grid.Row="0" Grid.Column="0"  Content="Connection String"/>
    <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding  Path=ConnectionString,UpdateSourceTrigger=PropertyChanged}" ></TextBox>
    <Button Grid.Row="1" Grid.Column="0"  Content="Load"  Command="{Binding BtnLoad,UpdateSourceTrigger=PropertyChanged}"></Button>
    <Button Grid.Row="1" Grid.Column="1"  Content="Confirm" Command="{Binding BtnConfirm}"></Button>
    <Button Grid.Row="1" Grid.Column="2"  Content="Add" Command="{Binding BtnAdd,UpdateSourceTrigger=PropertyChanged}"></Button>
</Grid>


    public MainViewModel(ILogRepository logRepository)
    {

        _logRepository = logRepository;

        _listOfLogs = new List<Log>();
        BtnAdd = new BtnAdd(AddLog);
        BtnConfirm = new BtnConfirm(ConfimLog, LogIsSelected);
        BtnLoad = new BtnLoad(LoadLogTable);

    }
    private bool LogIsSelected()
    {
        // return true;
        //Funktionerit im WPF nicht
        if (_selectedLogItem != null)
            return true;
        return false;
    }


Here is an example showing you how the can-execute method works.这是一个示例,向您展示了 can-execute 方法的工作原理。 The confirm button becomes enabled when there is text in the connection string input field.当连接字符串输入字段中有文本时,确认按钮将启用。 It's not exactly like your code, but you are referring to a list I don't see in your XAML.它与您的代码不完全一样,但您指的是我在您的 XAML 中没有看到的列表。

MainWindow.xaml (nothing in the code behind) MainWindow.xaml(后面的代码中没有)

<Window x:Class="FrameworkCanExecuteExample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:FrameworkCanExecuteExample"
        mc:Ignorable="d"
        Title="MainWindow"
        Width="300"
        Height="100">

    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <TextBlock Text="Connection string:" />
        <TextBox Grid.Column="1" Text="{Binding Path=ConnectionString, UpdateSourceTrigger=PropertyChanged}" />

        <Button Grid.Row="1" Grid.ColumnSpan="2"  Content="Confirm"  Command="{Binding BtnConfirm}" />
    </Grid>
</Window>

MainViewModel.cs主视图模型.cs

using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace FrameworkCanExecuteExample
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private string connectionString;

        public event PropertyChangedEventHandler PropertyChanged;

        public MainViewModel()
        {
            BtnConfirm = new RelayCommand(Confirm, CanConfirm);
        }

        public string ConnectionString
        {
            get => connectionString;
            set
            {
                connectionString = value;
                OnPropertyChanged();
            }
        }

        public ICommand BtnConfirm { get; }


        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private void Confirm(object parameter)
        {
        }

        private bool CanConfirm(object parameter)
        {
            return !string.IsNullOrWhiteSpace(connectionString);
        }
    }
}

RelayCommand.cs中继命令.cs

using System;
using System.Windows.Input;

namespace FrameworkCanExecuteExample
{
    public class RelayCommand : ICommand
    {
        private Action<object> execute;
        private Func<object, bool> canExecute;

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }

        public bool CanExecute(object parameter)
        {
            return canExecute == null || canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            execute(parameter);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM