| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
                            Tags
                            
                        
                          
                          - algorithm
 - 코딩테스트
 - Github
 - string
 - chashtag
 - git
 - Process
 - File
 - Binding
 - dotNET
 - Coding
 - tls
 - Visual Studio
 - C#
 - ListView
 - mysql
 - csharp
 - WPF
 - IValueConverter
 - logging
 - coding-test
 - Microsoft
 - nullable
 - windows10
 - convert
 - programmers
 - log
 - windows
 - .net
 - commit
 
                            Archives
                            
                        
                          
                          - Today
 
- Total
 
CHashtag
[C#/WPF] Listview SelectedItems command Binding (MVVM패턴) 본문
반응형
    
    
    
  WPF의 Listview는 많은 데이터를 표시할 수 있도록 도와주는 UI 요소 중 하나입니다.
이전에는 SelectedItems 속성을 바인딩하는 것이 불가능했지만, 최신 버전의 WPF에서는 Command로 처리할 수 있도록 업데이트되었습니다.
이번에는 Prism 라이브러리를 이용하여 Mvvm 패턴을 적용한 예제를 살펴보겠습니다.
Prism은 MVVM 패턴을 구현하는 데 도움이 되는 많은 기능을 제공합니다.
우선, Prism 라이브러리를 사용하려면 NuGet 패키지 관리자에서 Prism.Core 패키지를 설치해야 합니다.
설치 후, ListView와 Command를 바인딩하는 방법을 알아보겠습니다.
ListView의 Behaviors 클래스 가져오기
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:prism="http://prismlibrary.com/"
ListView에 대한 Behaviors 클래스를 사용하여 SelectedItems와 연결된 ICommand를 설정합니다.
<ListView Name="myListView" ItemsSource="{Binding MyItems}" SelectionMode="Multiple">
    <i:Interaction.Behaviors>
        <prism:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=myListView, Path=SelectedItems}" />
    </i:Interaction.Behaviors>
</ListView>
위의 코드에서 MyCommand는 ICommand를 구현한 ViewModel의 속성입니다.
이 속성은 SelectedItems 속성과 연결됩니다.
ViewModel에서 MyCommand 속성을 구현합니다.
public class MainViewModel : BindableBase
{
    private ObservableCollection<string> _myItems;
    private ICommand _myCommand;
    public ObservableCollection<string> MyItems
    {
        get { return _myItems; }
        set { SetProperty(ref _myItems, value); }
    }
    public ICommand MyCommand
    {
        get { return _myCommand; }
        set { SetProperty(ref _myCommand, value); }
    }
    public MainViewModel()
    {
        MyItems = new ObservableCollection<string>() { "Item 1", "Item 2", "Item 3", "Item 4" };
        MyCommand = new DelegateCommand<IList>(OnMyCommandExecuted);
    }
    private void OnMyCommandExecuted(IList selectedItems)
    {
        // do something with selected items
    }
}
위의 코드에서 DelegateCommand를 사용하여 MyCommand 속성을 구현합니다.
이 속성은 SelectedItems 속성과 연결됩니다. OnMyCommandExecuted 메서드에서는 선택된 항목을 처리합니다. 
이제 ListView의 SelectedItems를 ICommand와 바인딩하는 방법과 Prism 라이브러리를 사용하여 Mvvm 패턴을 구현하는 방법을 알아봤습니다. 이를 통해 ViewModel에서 ListView의 선택된 항목을 처리할 수 있습니다.
감사합니다.
반응형
    
    
    
  'C# > WPF' 카테고리의 다른 글
| [C#/WPF] Converter사용하여 bool 반대값 binding하기 (0) | 2023.02.20 | 
|---|---|
| [C# WPF] 다른 Control Property Binding (다른 컨트롤 속성 바인딩) (5) | 2021.12.03 | 
| [C#] [WPF] TextBox 숫자만 입력받는 법 (0) | 2021.11.29 | 
| [C#] [WPF] TextBox의 실시간 Binding(바인딩) (0) | 2021.09.16 | 
| [C#] [WPF] ComboBox Converter 사용방법 (0) | 2021.07.24 |