C# WPF mvvm按钮(ICommand)无法执行不工作

C# WPF mvvm按钮(ICommand)无法执行不工作,c#,wpf,mvvm,C#,Wpf,Mvvm,我有这个xaml和这个代码。 我的问题是确认没有改变。 CanExecute仅执行一次 <Label Grid.Row="0" Grid.Column="0" Content="Connection String"/> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=ConnectionString,UpdateSourceTrigger=PropertyChanged}" ><

我有这个xaml和这个代码。 我的问题是确认没有改变。 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>


公共MainViewModel(ILogRepository日志存储库)
{
_logRepository=logRepository;
_listOfLogs=新列表();
BtnAdd=新的BtnAdd(AddLog);
BtnConfirm=新BtnConfirm(已选择确认日志、登录);
BtnLoad=新的BtnLoad(LoadLogTable);
}
私有布尔逻辑选择()
{
//返回true;
//Funktionerit im WPF nicht
如果(_selectedLogItem!=null)
返回true;
返回false;
}

下面是一个示例,向您展示can execute方法的工作原理。当连接字符串输入字段中有文本时,确认按钮将启用。这与您的代码并不完全相同,但您所指的是我在XAML中没有看到的列表

MainWindow.xaml(代码隐藏中没有任何内容)

RelayCommand.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);
        }
    }
}
使用系统;
使用System.Windows.Input;
命名空间框架CanExecuteExample
{
公共类中继命令:ICommand
{
私人行动执行;
私人职能执行;
公共事件事件处理程序CanExecuteChanged
{
添加{CommandManager.RequerySuggested+=value;}
删除{CommandManager.RequerySuggested-=value;}
}
公共RelayCommand(执行操作,Func canExecute=null)
{
this.execute=execute;
this.canExecute=canExecute;
}
公共布尔CanExecute(对象参数)
{
返回canExecute==null | | canExecute(参数);
}
public void Execute(对象参数)
{
执行(参数);
}
}
}

欢迎来到堆栈溢出。你似乎对一些没有给我们看的代码有点问题。请通过编辑您的问题并将代码添加到您的问题中,向我们显示该代码。当命令能够执行更改时,可能需要引发命令的
CanExecuteChanged
事件。如果不这样做,按钮将不会知道它需要再次调用CanExecute。您可以从这些绑定中删除
updateSourceTracger=PropertyChanged
。Command属性永远不会创建新的Command对象并将其分配回viewmodel属性,因此无需确切地告诉它何时执行此操作。即使它愿意尝试,您的属性也可能是只读的。什么决定它是否可以执行?选择的Logisselected是私有的。我认为,要想让这一观点获得任何价值,它需要公开。您还需要一些用户界面交互,或者在检查之前。
<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>
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);
        }
    }
}
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);
        }
    }
}