0
這是跟我的問題here。看起來即使InkPresenter
離用戶最近,Image
元件也會接收到這些事件。有誰知道這是爲什麼?Z訂單和事件處理wpf
編輯:如果對不起,我沒有做的問題更清楚:
我已經attatched事件處理程序的InkPresenter
,這是Image
的兄弟。我已將InkPresenter
的ZIndex
設置爲2(其他元素的值爲ZIndex
)。我不確定爲什麼圖像接收到事件處理程序。我認爲具有最高ZIndex
的元素最接近用戶。因此,它將首先接收用戶生成的事件,但在這種情況下,Image
會收到它們。
我的代碼如下:
CustomImage.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)
{
// ...
}
Generic.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>
感謝您的回覆肯特:)我已經更新了問題 – Nilu 2009-07-23 02:15:22