c# - 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;
}
}
先感谢您。
解决方案
推荐阅读
- apache-spark - 从 pyspark.sql.dataframe.DataFrame 到 arraytype
- python - OpenCV 霍夫线
- javascript - addEventListener 与媒体冲突
- c++ - 如果打开了堆栈保护器,为什么clang会生成rsqrt?
- java - Gitlab 上 GCP App Engine 的 Maven 部署
- python - 从另一个嵌套类访问属性
- dictionary - 如何(不安全地)将地图反映为约束?
- python - 将文件夹中的图像插入数据框
- javascript - javascript:获取下个月的第一个工作日
- c++ - 正确制作 AI 循环?