티스토리 뷰

c#

[wpf/c#] mvvm light, CommandParameter 사용법

개발자 고포고 2022. 2. 24. 19:00
반응형

[wpf/c#] mvvm light, CommandParameter 사용법

 

#Command사용 시 Parameter를 넘길 수 있다.

 

#View(xaml) - CommandParameter="aaaa"등과 같이 값을 넣어준다.

      <TextBox Margin="10,0,0,0" Grid.Row="1" HorizontalAlignment="Left" Width="180" Height="30" Text="{Binding TextBoxText,UpdateSourceTrigger=PropertyChanged}" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="TextChanged" >
                    <i:InvokeCommandAction Command="{Binding OnTextChangedCommand}" CommandParameter="aaaaa" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </TextBox>

 

#ViewModel 

 //정의
 public RelayCommand<string> OnTextChangedCommand { get; set; }
        
        private void OnTextChangedCommandAction(string msg)
        {
            MessageBox.Show(TextBoxText);
        }
        
 //초기화
 OnTextChangedCommand = new RelayCommand<string>((msg)=> OnTextChangedCommandAction(msg), null);

 

이렇게 간단하게 쓸 수 있다.

 

#mvvm #commandparameter #command #wpf #c#

반응형
댓글
반응형