Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MouseLeftButtonUp不会为WPF控件模板中的ScrollViewer触发_C#_Wpf - Fatal编程技术网

C# MouseLeftButtonUp不会为WPF控件模板中的ScrollViewer触发

C# MouseLeftButtonUp不会为WPF控件模板中的ScrollViewer触发,c#,wpf,C#,Wpf,在WPF和C中,我试图为ScrollViewer设置鼠标拖动滚动功能,该功能包含在文档查看器的控件模板中。问题是:我没能让鼠标按钮不开火 它基本上是默认的DocumentViewer模板,并修改了一些功能。以下是XAML的概要: <Style x:Key="DocumentViewerStyle1" BasedOn="{x:Null}" TargetType="{x:Type DocumentViewer}"> <!--...—> <Setter Prope

在WPF和C中,我试图为ScrollViewer设置鼠标拖动滚动功能,该功能包含在文档查看器的控件模板中。问题是:我没能让鼠标按钮不开火

它基本上是默认的DocumentViewer模板,并修改了一些功能。以下是XAML的概要:

<Style x:Key="DocumentViewerStyle1" BasedOn="{x:Null}" TargetType="{x:Type DocumentViewer}">
<!--...—>
    <Setter Property="ContextMenu" Value="{x:Null}" />  <!--So does not mess up right click, if I use that-->
<!--...-->
    <ControlTemplate TargetType="{x:Type DocumentViewer}">
<!--...-->
        <ScrollViewer x:Name="PART_ContentHost" CanContentScroll="True"
            IsHitTestVisible="True" HorizontalScrollBarVisibility="Auto" 
            Grid.Row="1" Loaded ="OnScrollViewerLoaded" />
        <DockPanel Grid.Row="1" >
<!-...-->
</ControlTemplate>
</Style>


等及

private void OnScrollViewerLoaded(object sender, RoutedEventArgs e)
{
    nomad = (ScrollViewer)sender;
    nomad.AddHandler(MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseButtonDown), true);
    nomad.AddHandler(MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseButtonUp), true);
    nomad.AddHandler(MouseMoveEvent, new MouseEventHandler(OnMouseMove), true);
}
例如,OnMouseButtonUp事件处理程序是

private void OnMouseButtonUp(object sender, MouseButtonEventArgs e)
{
    nomad.Cursor = Cursors.IBeam;
    nomad.ReleaseMouseCapture();
}

我尝试过这里的各种方法:对我的三个鼠标事件使用预览事件没有帮助。对ScrollViewer设置Focusable=“False”或设置其背景没有帮助。有什么建议吗?谢谢

我始终确保的第一件事是显式地将背景设置为非空值(例如,Brush.Transparent)或在具有定义背景的控件模板中的所有组件后面添加边框,否则将不会触发鼠标事件我在看到您的想法后尝试了进一步的背景实验,但没有任何区别。不过谢谢你。
private void OnMouseButtonUp(object sender, MouseButtonEventArgs e)
{
    nomad.Cursor = Cursors.IBeam;
    nomad.ReleaseMouseCapture();
}