gpt4 book ai didi

c# - C# 和 WPF 中的命令

转载 作者:行者123 更新时间:2023-12-02 17:01:49 25 4
gpt4 key购买 nike

我的代码非常简单,但它不起作用。

我有一个命令:

public class SetYesCommand : ICommand , INotifyPropertyChanged
{
public event EventHandler CanExecuteChanged;
public event PropertyChangedEventHandler PropertyChanged;

ViewModelBase view = new ViewModelBase();

public bool CanExecute(object parameter)
{
return true;
}

public void Execute(object parameter)
{
view.Sname = "Yes";
MessageBox.Show("Command works");
onpropertychanged("Sname");
}

private void onpropertychanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
}
}

这是我的基础 View 模型类:

 public class ViewModelBase : INotifyPropertyChanged
{
private string _s;
public string Sname {
get { return _s; }
set { _s = value;
onPropertyChanged("Sname");
}

}

private void onPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}
}

这是我的 XAML 代码:

<Window x:Class="Test.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:Test"
mc:Ignorable="d"
xmlns:viewmodel="clr-namespace:Test.ViewModel"
xmlns:commands="clr-namespace:Test.ViewModel.Commands"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<viewmodel:ViewModelBase x:Key="base" ></viewmodel:ViewModelBase>
<viewmodel:Converters x:Key="convert" ></viewmodel:Converters>
<commands:SetYesCommand x:Key="setyes"/>
</Window.Resources>
<StackPanel>
<Button Width="100" Height="30" Command="{StaticResource setyes}"/>
<TextBox DataContext="{StaticResource base}" Text="{Binding Sname , Mode=TwoWay}"/>
<CheckBox DataContext="{StaticResource base}" IsChecked="{Binding Sname , Mode=TwoWay , Converter={StaticResource convert}}" />
<TextBox />
</StackPanel>
</Window>

如您所见,我只是将 UI 中的两个元素绑定(bind)到 View 模型基类中的字符串属性,并且我为 UI 中的按钮取消命令。该命令有效,因为如您所见,我使用消息框对其进行了检查,但我的 UI 中的其他两个元素(文本框和复选框)的值没有改变。

最佳答案

您正在创建 ViewModelBase 的新实例在你的指挥下。应该创建命令的是 View 模型,而不是相反。

ICommand 的典型实现接口(interface)通常接受 Action<object>被执行和一个Predicate<object>决定是否执行命令:

public class SetYesCommand : ICommand
{
public event EventHandler CanExecuteChanged;
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;

public SetYesCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}

public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute(parameter);
}

public void Execute(object parameter)
{
if (_execute != null)
_execute(parameter);
}
}

然后在 View 模型中实现逻辑:

public class ViewModelBase : INotifyPropertyChanged
{
private string _s;
public string Sname
{
get { return _s; }
set
{
_s = value;
onPropertyChanged("Sname");
}

}

public ViewModelBase()
{
TheCommand = new SetYesCommand(OnCommandExecuted, null);
}

private void OnCommandExecuted(object parameter)
{
Sname = "Yes";
MessageBox.Show("Command works");
}

public ICommand TheCommand { get; private set; }

private void onPropertyChanged(string propertyname)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}

public event PropertyChangedEventHandler PropertyChanged;
}

在 View 中,您绑定(bind)到返回 ICommand 实例的 View 模型的命令属性。实现:

<Button Width="100" Height="30" DataContext="{StaticResource base}" Command="{Binding TheCommand}"/>

关于c# - C# 和 WPF 中的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53781413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com