gpt4 book ai didi

c# - CommandParameter 如何在 MVVM 中工作?

转载 作者:行者123 更新时间:2023-11-30 21:53:00 30 4
gpt4 key购买 nike

我想在我的类 CommandProvider 中实现 CommandParameter,它用于命令(按钮等)并继承自 ICommand,但我不明白如何实现它。示例:

XAML

<TreeViewItem Header="Playlist" ItemsSource="{Binding ItemSourceTree}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding Path=NewPlaylist}"
CommandParameter="{Binding Path=NamePlaylist}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TreeViewItem.ItemTemplate>
<DataTemplate DataType="{x:Type local:PlaylistDB}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=NamePlaylist}">
</TextBlock>
</StackPanel>
</DataTemplate>
</TreeViewItem.ItemTemplate>
</TreeViewItem>

控制台显示,找不到 NamePlaylist。

并将函数链接到 Binding NewPlaylist:

public ICommand NewPlaylist { get { return new CommandProvider((obj) => DoubleClickTest(obj)); } }

函数

public void DoubleClickTest(object obj)
{
var tmp = obj as string;
Console.WriteLine(tmp);
}

所以我需要修改我的类 CommandProvider 以正确获取参数?我该怎么做?

CommandProvider

public class CommandProvider : ICommand
{
#region Constructors

public CommandProvider(Action<object> execute) : this(execute, null) { }

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

#endregion

#region ICommand Members

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute(parameter) : true;
}

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

public void OnCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}

#endregion

private readonly Action<object> _execute = null;
private readonly Predicate<object> _canExecute = null;
}

播放列表数据库

public class PlaylistDB
{
public string NamePlaylist { get; set; }
}

我想在我的函数 DoubleClickTest() 中检索 NamePlaylist,并且我想在 CommandParameter 中传递它。我该怎么做?

最佳答案

使用下面的类接受 commandparameters 使用 ICommand,

public class DelegateCommand: ICommand
{
#region Constructors

public DelegateCommand(Action<object> execute)
: this(execute, null) { }

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

#endregion

#region ICommand Members

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
return _canExecute != null ? _canExecute(parameter) : true;
}

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

public void OnCanExecuteChanged()
{
CanExecuteChanged(this, EventArgs.Empty);
}

#endregion

private readonly Action<object> _execute = null;
private readonly Predicate<object> _canExecute = null;
}

用法:

public ICommand CloseCommand
{
get
{
return new DelegateCommand((obj)=>CloseMethod(obj));
}
}

obj为上例中传入的命令参数

关于c# - CommandParameter 如何在 MVVM 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34272982/

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