WPF MVVM ICommnad 与 ListView

编程


你好,

我有一个关于 WPF 中的 ICommand 和 MVVM 的问题。

我发现处理事件的最常见方法是使用接口 ICommand。

据我所知,如果我想向按钮添加命令,我会执行以下操作:
<Button Command="{MyCommand}" />

但我不明白如何为 SelectionChanged 创建命令或 ListView 的命令?

有谁知道那里有什么好的例子,或者可以帮助我编写一些简单的代码?

————————————————– ————————————————-

你好,我们又见面了,
我在 listView 上执行 SelectionChanged 时仍然遇到问题。
我找到了这个帖子: http://blog.fossmo.net/post/How-to-create-an-attached-property-in-WPF-using-a-ComboBox.aspx[^] 但我不确定他的意思:“MyCommand 可以是来自 CAG 的 DelegateCommand。” CAG 课程会是什么样子?

解决方案2

首先,ICommand并不是支持事件的机制。 您应该通过它在 WPF 或 Silverlight 中执行基于命令的功能。 我这么说的原因是因为它超出了事件应该做的事情 – 使用命令基础结构,您还可以通过确定某些事情是否会发生来影响 UI,因为 ICommand 接口还支持 CanExecute 方法。 当某些东西支持 ICommand 时,UI 实际上会考虑此方法,这样当 CanExecute 返回 false 时您就无法按下按钮。

正如您现在所知,您无法将 ICommand 绑定到更改的选择,因为它不支持命令绑定。 乍一看,这似乎是一个真正的问题,但有许多解决方案支持这一点。 最好的实现可能是 MVVM Light 中的 EventToCommand 支持,您可以在其中找到 这里[^]。

Laurent 演示了如何使用此行为 这里[^]。

解决方案1

我遇到的最好的例子是使用附加属性。 在 Claus Conrads 博客上可以找到一个很好的例子 WPF ListView MVVM 和缺少 ICommand[^]。 附加属性可用于各种用途,并且绝对值得使用。

希望这可以帮助

解决方案4

For handling the SelectionChanged event in a ListView, there are two possible approaches: either by attaching a behavior or by using the Interaction framework.

For complete solution,I have created an example for it. follow the following steps:

==> Install Nuget package of "Microsoft.Xaml.Behaviors"

==>MainWindow.Xaml will be like below

<Window x:Class="WpfApp3.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:WpfApp3"
         xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ListView ItemsSource="{Binding Items}" 
          SelectedItem="{Binding SelectedItem}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <!-- ListView content here -->
        </ListView>

    </Grid>
</Window>

==>MainWindow.Xaml.cs

using System.Windows;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }
}

==>I've developed a ViewModel tailored for the provided XAML, facilitating the binding of commands with the view.

using System;
using System.Collections.Generic;
using System;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace WpfApp3
{
    public class MainViewModel
    {
        public ObservableCollection<string> Items { get; set; }

        public ICommand SelectionChangedCommand { get; set; }

        public MainViewModel()
        {
            Items = new ObservableCollection<string>
            {
                "Item 1",
                "Item 2",
                "Item 3"
            };

            SelectionChangedCommand = new RelayCommand(OnSelectionChanged);
        }

        private void OnSelectionChanged(object selectedItem)
        {
            // Handle selection changed logic here
            Console.WriteLine("Selected item: " + selectedItem);
        }
    }
}

==>RelayCommand.cs class 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WpfApp3
{
    public class RelayCommand : ICommand
    {
        private readonly Action<object> _execute;
        private readonly Func<object, bool> _canExecute;

        public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }

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

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

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

コメント

タイトルとURLをコピーしました