카테고리 없음

[씨#] WPF catch 그리드와 그의 모든 자식 레이블, 텍스트 상자 등을 클릭합니다.

행복을전해요 2021. 2. 24. 15:35

모든 컨트롤에 공통적 인 이벤트를 사용하기 만하면됩니다. 아마도이 시나리오에 가장 적합한 것은 UIElement.PreviewMouseDown이벤트 일 것 입니다. 이 시도:

<StackPanel UIElement.PreviewMouseDown="StackPanel_PreviewMouseDown">
    <Label Content="I'm a Label" />
        <Button Content="I'm a Button" />
            <CheckBox Content="I'm a CheckBox" />
            </StackPanel>
            

s가 그것을 소비 Preview...하기 전에 그것을 잡을 수 있도록 이벤트 중 하나를 사용해야 Button합니다. UIElement.MouseDown이벤트는 Button바로 그 이유 때문에 s 와 작동하지 않습니다 . 그러나 예를 들어 이벤트 Preview...와 같은 다른 방법을 사용할 수 있습니다 UIElement.PreviewLeftMouseButtonDown.

-------------------

샘플 코드를 줄 수 있습니까?

내 이해에서 이것을 사용할 수 있으며 그리드 내부의 모든 클릭을 캡처합니다.

.xaml

<Grid MouseDown="Grid_MouseDown">
<Label MouseDown="Grid_MouseDown" />
<Button MouseDown="Grid_MouseDown"/>
<Button MouseDown="Grid_MouseDown"/>
</Grid>

.xaml.cs

private Grid_MouseDown(object sender,MouseButtonEventArgs e)
{
    if(X==true)
        {
              //doSomething
                  }
                      else
                          {
                               //do SomethingElse
                                   }
                                   }
                                   

편집 : 어때요?



출처
https://stackoverflow.com/questions/22080083