Nice programing

WPF의 명령을 컨트롤의 두 번 클릭 이벤트 처리기에 바인딩하는 방법은 무엇입니까?

nicepro 2020. 11. 25. 21:13
반응형

WPF의 명령을 컨트롤의 두 번 클릭 이벤트 처리기에 바인딩하는 방법은 무엇입니까?


내 ViewModel의 명령에 textblock (또는 잠재적으로 이미지-어느 쪽이든 사용자 컨트롤)의 두 번 클릭 이벤트를 바인딩해야합니다.

TextBlock.InputBindings가 내 명령에 올바르게 바인딩되지 않는 것 같습니다.


Marlon Grech의 첨부 된 명령 동작을 사용해보십시오 .


<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
</Button.InputBindings>
</Button>

http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx


간단합니다. MVVM 방식을 사용하겠습니다. 여기서 배우기 쉽고 강력한 MVVM Light를 사용하고 있습니다.

1. 다음 줄에 xmlns 선언을 입력합니다.

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"  
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
                                   assembly=GalaSoft.MvvmLight.Extras.WPF4"

2. 텍스트 블록을 다음과 같이 정의하십시오.

<textBlock text="Text with event">
   <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <GalaSoft_MvvmLight_Command:EventToCommand 
                             Command="{Binding Edit_Command}"/>
      </i:EventTrigger>
   </i:Interaction.Triggers>
</textBlock>

3. 그런 다음 뷰 모델에 명령 코드를 작성하십시오 !!!

ViewModel1.cs

Public RelayCommand Edit_Command
{
   get;
   private set;
}

Public ViewModel1()
{
   Edit_Command=new RelayCommand(()=>execute_me());
}

public void execute_me()
{
   //write your code here
}

Real ERP 응용 프로그램에서 사용 했으므로 그것이 당신에게 효과가 있기를 바랍니다.


또한 listview의 MouseDoubleClick 이벤트를 ViewModel의 명령에 바인딩해야하는 비슷한 문제가있었습니다.

내가 생각 해낸 가장 간단한 해결책은 원하는 명령 바인딩이있는 더미 버튼을 넣고 MouseDoubleClick 이벤트의 이벤트 처리기에서 버튼 명령의 Execute 메서드를 호출하는 것입니다.

.xaml

 <Button Visibility="Collapsed" Name="doubleClickButton" Command="{Binding Path=CommandShowCompanyCards}"></Button>
                <ListView  MouseDoubleClick="ListView_MouseDoubleClick" SelectedItem="{Binding Path=SelectedCompany, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Margin="0,10,0,0" ItemsSource="{Binding Path=CompanyList, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" HorizontalContentAlignment="Stretch" >

코드 숨김

     private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                doubleClickButton.Command.Execute(null);
            }

간단하지는 않지만 정말 간단하고 작동합니다.

참고 URL : https://stackoverflow.com/questions/1293530/how-to-bind-a-command-in-wpf-to-a-double-click-event-handler-of-a-control

반응형