Note that there are some explanatory texts on larger screens.

plurals
  1. POZ order and event handling wpf
    primarykey
    data
    text
    <p>This is a follow up to my question <a href="https://stackoverflow.com/questions/1162963/z-order-in-between-image-and-inkpresenter">here</a>. It seems that even though the <code>InkPresenter</code> is closest to the user, the <code>MouseDown</code>/<code>MouseMove</code>/<code>MouseUp</code> events are received by the <code>Image</code> element. Does anyone know why that is?</p> <p><strong>Edit:</strong> Sorry if I didn't make the question clearer: </p> <p>I have attatched event handlers to the <code>InkPresenter</code>, which is a sibling of the <code>Image</code>. I have set the <code>ZIndex</code> of the <code>InkPresenter</code> to 2 (other elements have 0 as the <code>ZIndex</code>). I'm not sure why the event handlers are received by the the Image. I assumed that the element which has the highest <code>ZIndex</code> would be closest to the user. Thus, it would be the first to receive the <code>MouseDown</code>/<code>MouseMove</code>/<code>MouseUp</code> events generated by the user however in this case the <code>Image</code> receives them.</p> <p>My code is as follows:</p> <p><strong>CustomImage.cs</strong></p> <pre><code>[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) { // ... } </code></pre> <p><strong>Generic.xaml</strong></p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CustomImage"&gt; &lt;Style TargetType="{x:Type local:CustomImage}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type local:CustomImage}"&gt; &lt;ControlTemplate.Resources&gt; &lt;local:StringtoImageSource x:Key="ImageSourceConverter"/&gt; &lt;/ControlTemplate.Resources&gt; &lt;Canvas Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"&gt; &lt;Image x:Name="PART_Image" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Source, Converter={StaticResource ImageSourceConverter}}"/&gt; &lt;InkPresenter Canvas.ZIndex="2" x:Name="PART_InkPresenter" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"/&gt; &lt;/Canvas&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload