एक सूचीदृश्य खोजें MVVM/WPF C#


जब मैं एक बटन दबाता हूं तो मैं एक सूचीदृश्य खोजना चाहता हूं और इसे स्क्रॉल करके पाया गया आइटम प्रदर्शित करना चाहता हूं

ViewModel पैटर्न का उपयोग करके WPF ListView को सरल बनाना

मैंने क्या प्रयास किया है:

मुझे ऐसा कोई उदाहरण नहीं मिला जो मेरी मदद कर सके

समाधान 1

हम्म्म… एक त्वरित गूगल खोज: डब्ल्यूपीएफ सूचीदृश्य खोज एमवीवीएम उदाहरण – Google[^] कई उदाहरण सामने आते हैं, जैसे: एमवी-वीएम के साथ डब्ल्यूपीएफ में सूची फ़िल्टरिंग[^]जबकि ए ListBox प्रयोग किया जाता है, वही सिद्धांत लागू होता है। यदि आप इससे खुश नहीं हैं, तो ऊपर दी गई खोज में से चुनने के लिए और भी बहुत कुछ है।

समाधान 2

To search a ListView in an MVVM (Model-View-ViewModel) WPF (Windows Presentation Foundation) application using C#, you can follow these steps:

1. **Implement a Search Property in ViewModel**: Add a property in your ViewModel to store the search query entered by the user.

2. **Bind Search Property to TextBox**: Bind the Search property to a TextBox in your View to allow users to input search queries.

3. **Filter the ListView**: Implement a mechanism in your ViewModel to filter the items displayed in the ListView based on the search query.

Here's a basic example to demonstrate these steps:

ViewModel (ViewModel.cs):

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

public class ViewModel : INotifyPropertyChanged
{
    private string _searchQuery;
    public string SearchQuery
    {
        get { return _searchQuery; }
        set
        {
            if (_searchQuery != value)
            {
                _searchQuery = value;
                OnPropertyChanged(nameof(SearchQuery));
                FilterListView();
            }
        }
    }

    public ObservableCollection<string> OriginalItems { get; set; }
    public ObservableCollection<string> FilteredItems { get; set; }

    public ViewModel()
    {
        // Initialize your original items collection
        OriginalItems = new ObservableCollection<string>
        {
            "Item 1",
            "Item 2",
            "Item 3",
            // Add more items as needed
        };

        // Initialize filtered items collection
        FilteredItems = new ObservableCollection<string>(OriginalItems);
    }

    private void FilterListView()
    {
        if (string.IsNullOrWhiteSpace(SearchQuery))
        {
            // If search query is empty, show all original items
            FilteredItems = new ObservableCollection<string>(OriginalItems);
        }
        else
        {
            // Filter original items based on search query
            FilteredItems.Clear();
            foreach (var item in OriginalItems.Where(i => i.Contains(SearchQuery)))
            {
                FilteredItems.Add(item);
            }
        }
    }

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


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 SearchQuery, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10"/>
        <ListView ItemsSource="{Binding FilteredItems}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>


Make sure to set the DataContext of your View to an instance of your ViewModel. You can do this in the code-behind of your View (MainWindow.xaml.cs) or using a DI (Dependency Injection) framework like Prism or MVVM Light. Also, ensure that your ViewModel class implements the INotifyPropertyChanged interface to update the UI when the SearchQuery property changes.

コメント

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