ListView के साथ WPF MVVM ICommnad


नमस्ते,

मेरे पास WPF में ICommand और MVVM के बारे में एक प्रश्न है।

मैंने देखा है कि घटनाओं को संभालने का सबसे आम तरीका इंटरफ़ेस ICommand का उपयोग करना है।

मैं समझता हूं कि यदि मैं किसी बटन में कमांड जोड़ना चाहता हूं, तो मैं कुछ इस तरह करता हूं:
<Button Command="{MyCommand}" />

लेकिन मुझे समझ नहीं आ रहा है कि सिलेक्शनचेंज्ड या लिस्ट व्यू के लिए कुछ कमांड कैसे बनाया जाए?

क्या कोई वहां कोई अच्छा उदाहरण जानता है, या कुछ सरल कोड के साथ मेरी मदद कर सकता है?

————————————————– ————————————————–

फिर से नमस्कार,
मुझे सूची दृश्य में चयन परिवर्तन के आईकमांड कार्यान्वयन में अभी भी समस्या है।
मुझे यह पोस्ट मिली: http://blog.fossmo.net/post/How-to-create-an-attached-property-in-WPF-using-a-ComboBox.aspx[^] लेकिन मुझे यकीन नहीं है कि उनका क्या मतलब है: “माईकमांड सीएजी से एक डेलीगेटकमांड हो सकता है।” CAG कक्षा कैसी दिख सकती है?

समाधान 2

सबसे पहले, ICommand घटनाओं का समर्थन करने वाला तंत्र नहीं है। यह वह तंत्र है जिसके द्वारा आपको WPF या सिल्वरलाइट में कमांड आधारित कार्यक्षमता निष्पादित करनी चाहिए। मैं ऐसा इसलिए कह रहा हूं क्योंकि यह किसी ईवेंट को जो करना चाहिए उससे कहीं आगे जाता है – कमांडिंग इंफ्रास्ट्रक्चर के साथ, आप यह निर्धारित करके यूआई को भी प्रभावित कर सकते हैं कि कुछ हो सकता है या नहीं क्योंकि ICommand इंटरफ़ेस CanExecute पद्धति का भी समर्थन करता है। जब कोई चीज़ ICommand का समर्थन करती है, तो UI वास्तव में इस विधि को ध्यान में रखेगा, ताकि जब CanExecute गलत रिटर्न दे तो आप बटन नहीं दबा सकें।

जैसा कि आप अब जानते हैं, आप ICommand को बदले हुए चयन से नहीं जोड़ सकते क्योंकि यह कमांड बाइंडिंग का समर्थन नहीं करता है। सबसे पहले, यह एक वास्तविक समस्या प्रतीत होगी, लेकिन ऐसे कई समाधान हैं जो इसका समर्थन करते हैं। संभवतः सबसे अच्छा कार्यान्वयन एमवीवीएम लाइट में इवेंटटूकमांड समर्थन है जिसे आप पा सकते हैं यहाँ[^].

लॉरेंट इस व्यवहार का उपयोग करके प्रदर्शित करता है यहाँ[^].

समाधान 1

सबसे अच्छे उदाहरण जो मुझे मिले हैं उनमें संलग्न संपत्तियों का उपयोग किया गया है। इसका एक अच्छा उदाहरण क्लॉस कॉनराड्स ब्लॉग पर पाया जा सकता है WPF ListView MVVM और अनुपलब्ध ICommand[^]. संलग्न संपत्तियों का उपयोग सभी प्रकार की चीजों के लिए किया जा सकता है और निश्चित रूप से इनके साथ सहज होना उचित है।

उम्मीद है ये मदद करेगा

समाधान 3

To use WPF MVVM with ICommand and a ListView, you typically bind a command to an event in your ViewModel and handle the logic for that command. 
Here's a basic example of how you can achieve this with a ListView:

ViewModel (ViewModel.cs):

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

public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<string> Items { get; set; }

    public ICommand AddItemCommand { get; }

    private string _newItemText;
    public string NewItemText
    {
        get { return _newItemText; }
        set
        {
            if (_newItemText != value)
            {
                _newItemText = value;
                OnPropertyChanged(nameof(NewItemText));
            }
        }
    }

    public ViewModel()
    {
        Items = new ObservableCollection<string>();

        // Initialize command
        AddItemCommand = new RelayCommand(AddItem, CanAddItem);
    }

    private void AddItem(object parameter)
    {
        Items.Add(NewItemText);
        NewItemText = string.Empty; // Clear the TextBox after adding item
    }

    private bool CanAddItem(object parameter)
    {
        // Enable the command if the NewItemText is not null or empty
        return !string.IsNullOrEmpty(NewItemText);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}


RelayCommand.cs (for implementing ICommand):

using System;
using System.Windows.Input;

public class RelayCommand : ICommand
{
    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute, Predicate<object> 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) => _canExecute == null || _canExecute(parameter);
    public void Execute(object parameter) => _execute(parameter);
}


View (MainWindow.xaml):

<Window x:Class="YourNamespace.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:YourNamespace"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox Text="{Binding NewItemText, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10"/>
        <Button Content="Add Item" Command="{Binding AddItemCommand}" CommandParameter="{Binding NewItemText}" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10"/>
        <ListView ItemsSource="{Binding Items}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10"/>
    </Grid>
</Window>


This example demonstrates a simple scenario where a user can add items to a ListView using a TextBox and a Button. The AddItemCommand is bound to the Button's Command property, and the TextBox's Text property is bound to the NewItemText property in the ViewModel. When the Button is clicked, the AddItemCommand is executed, adding the text from the TextBox to the Items collection in the ViewModel. The CanAddItem method in the ViewModel determines whether the command can be executed based on the value of the NewItemText property.

コメント

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