次のようなイベントをオーバーライドする UserControl があります。
public class MyControl: UserControl
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
// handle mouse event up
}
}
このコントロールを別の UserControl -> Grid に追加します。この Grid には MouseUp が登録されています。
public class MyParent: UserControl
{
private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
{
// handle grid mouse event
}
}
そして MyParent XAML では、私は単純に次のことをしています:
<UserControl ... >
<Grid Name="Grid" MouseUp="Grid_MouseUp">
<my:MyControl Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" />
</Grid>
</UserControl>
MyControl の上でマウスを離すと、イベントが Grid によってキャプチャされ、MyControl にルーティングされないのはなぜですか?
MyControl クラス内で MouseUp イベントを受け取るにはどうすればよいですか?
編集 MouseDown ではすべてが期待どおりに機能しますが、MouseUp のみが機能しません...そして両方のイベントが親グリッドにも登録されているので、違いは何ですか?
EDIT2
OK、問題が見つかったと思います。MyControl を MyParent -> Grid に XAML で直接追加すると、すべて正常に動作しますが、プログラムで「MyParentInstance.Grid.Children.Add(MyControlInstance)」を追加すると、上記の問題が発生します。 .
コントロールを追加するコードは正しいですか?
ありがとう。