0

这是对我的问题的跟进。似乎即使InkPresenter最接近用户,//MouseDown事件也被元素接收。有谁知道这是为什么?MouseMoveMouseUpImage

编辑:对不起,如果我没有让问题更清楚:

我已将事件处理程序附加InkPresenterImage. 我已将 的 设置ZIndexInkPresenter2(其他元素的 0 为ZIndex)。我不确定为什么 Image 会接收到事件处理程序。我假设具有最高的元素ZIndex将最接近用户。因此,它将是第一个接收用户生成的MouseDown//事件,但是在这种情况下MouseMove接收它们。MouseUpImage

我的代码如下:

自定义图像.cs

[TemplatePart(Name="PART_InkPresenter",Type=typeof(InkPresenter))]
[TemplatePart(Name="PART_Image",Type=typeof(Image))]
public class CustomImage : Control
{
    public static DependencyProperty SourceProperty;

    public string Source
    {
        get { return (string)GetValue(SourceProperty); }
        set { SetValue(SourceProperty,value); }
    }

    private InkPresenter _presenter;
    private Image _image;


    static CustomImage()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomImage),
                new FrameworkPropertyMetadata(typeof(CustomImage)));

        SourceProperty = DependencyProperty.Register("Source",
                typeof(string), typeof(CustomImage));
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        _presenter = base.GetTemplateChild("PART_InkPresenter") as InkPresenter;
        _image = base.GetTemplateChild("PART_Image") as Image;

        _presenter.MouseDown += new MouseButtonEventHandler(_presenter_MouseDown);
        _presenter.MouseMove += new MouseEventHandler(_presenter_MouseMove);
        _presenter.MouseUp += new MouseButtonEventHandler(_presenter_MouseUp);


    }

    void _presenter_MouseUp(object sender, MouseButtonEventArgs e)
    {
        // ...
    }

    void _presenter_MouseMove(object sender, MouseEventArgs e)
    {
        // ...
    }

    void _presenter_MouseDown(object sender, MouseButtonEventArgs e)
    {
       // ...
    }

通用的.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomImage">
    <Style TargetType="{x:Type local:CustomImage}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomImage}">
                    <ControlTemplate.Resources>
                        <local:StringtoImageSource x:Key="ImageSourceConverter"/>
                    </ControlTemplate.Resources>
                    <Canvas Width="{TemplateBinding Width}" 
                            Height="{TemplateBinding Height}">               
                         <Image x:Name="PART_Image" 
                                Width="{TemplateBinding Width}" 
                                Height="{TemplateBinding Height}" Source="{Binding 
                                 RelativeSource={RelativeSource TemplatedParent}, 
                                 Path=Source, 
                                 Converter={StaticResource ImageSourceConverter}}"/>
                        <InkPresenter Canvas.ZIndex="2"  
                                x:Name="PART_InkPresenter" 
                                Width="{TemplateBinding Width}" 
                                Height="{TemplateBinding Height}"/>
                    </Canvas>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
4

1 回答 1

1

因为他们没有被其他任何东西处理?如果没有人处理该事件(设置e.Handledtrue),他们将继续遍历 WPF 可视化树。这就是路由事件应该做的事情。

您还没有真正提供足够的信息来知道是否应该处理它们。

于 2009-07-22T10:32:30.793 回答