首页 > 解决方案 > WPF:ManipulationStarting 未触发,但 MouseWheel 工作正常

问题描述

我正在尝试放大和缩小我在 WPF 中的控制。它适用于鼠标滚轮,但不会触发操作消息。我曾尝试将 IsManipulationEnabled 属性设置为父母,但没有受到影响。

这是我的代码。

XAML
<Style TargetType="{x:Type controls:MyControl}">
   <Setter Property="Focusable" Value="True" />
   ...
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type controls:MyControl}">
            <Grid>
               <ContentPresenter x:Name="PART_ContentPresenter"
                        Content="{TemplateBinding Content}" ... />
           </Grid>
   ... // other settings here

C#
public class MyControl: ContentControl
{
  private ContentPresenter _contentPresenter;
  public override void OnApplyTemplate()
  {
     _contentPresenter = Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
     if (_contentPresenter != null)
     {
        _contentPresenter.MouseWheel += OnMouseWheelMessage;
        _contentPresenter.IsManipulationEnabled = true;
        _contentPresenter.ManipulationStarting += OnManipulationStarting;
        _contentPresenter.ManipulationDelta += OnManipulationDelta;
     }
  }
  private void OnManipulationStarting(object sender, ManipulationStartingEventArgs e)
  {
      Trace.WriteLine("ManipulationStarting is fired");
      if (e.Source != _contentPresenter)
        return;
      e.ManipulationContainer = _contentPresenter;
      e.Handled = true;
  }
  private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
  {
      Trace.WriteLine($"ManipulationDelta : ");
      Trace.WriteLine($"                    IsInertial = {e.IsInertial}");
      Trace.WriteLine($"                    Scale.X = {e.DeltaManipulation.Scale.X}");
      Trace.WriteLine($"                    Scale.Y = {e.DeltaManipulation.Scale.Y}");
      Trace.WriteLine($"                    Origin.Y = {e.ManipulationOrigin.Y}");
      Trace.WriteLine($"                    Origin.X = {e.ManipulationOrigin.X}");
      Trace.WriteLine($"                    Translation.X = {e.DeltaManipulation.Translation.X}");
      Trace.WriteLine($"                    Translation.Y = {e.DeltaManipulation.Translation.Y}");
  }
  public void OnMouseWheelMessage(object sender, MouseWheelEventArgs e)
  {
      Trace.WriteLine($"MouseWheel is fired");
      base.OnMouseWheel(e);
      e.Handled = true;
  }
}

先感谢您。

标签: c#wpf

解决方案


推荐阅读