【问题标题】:Stop WPF InkBrush from causing a scroll event阻止 WPF InkBrush 引发滚动事件
【发布时间】:2019-08-28 17:40:07
【问题描述】:

应用程序

我正在开发 EHR 应用程序。我正在处理的页面允许用户使用InkCanvasInkBrush 在患者的笔记上绘图。当用户使用鼠标绘图时,一切正常。 然而,当他们使用触摸屏来绘制滚动条时,也会被操纵。这会导致页面在绘制时滚动。

我的尝试

到目前为止,我已经尝试处理诸如TouchDownTouchUpTouchMove 之类的触摸事件,主要使用e.handled = true

应用程序代码(抱歉,代码很多!)

ScribblePad.xaml(Border + InkCanvas 代码的 sn-p)

<Border Grid.Column="1"
                Margin="1,1,8,8"
                Background="White"
                BorderBrush="Black"
                BorderThickness="1"
                Cinch:SingleEventCommand.RoutedEventName="MouseDown"
                Cinch:SingleEventCommand.TheCommandToRun="{Binding 
AddTextCommand}"
                RequestBringIntoView="Border_RequestBringIntoView">
          <Border.Effect>
            <DropShadowEffect />
          </Border.Effect>

          <InkCanvas x:Name="NewScribbles"
                     Width="{Binding Parent.PageWidth}"
                     Height="{Binding Parent.PageHeight}"
                     DefaultDrawingAttributes="{Binding Parent.SelectedInkBrush}"
                     EditingMode="{Binding Parent.InkEditMode}"
                     Strokes="{Binding NewStrokes}">
            <Grid>
              <Grid.Style>
                <Style TargetType="{x:Type Grid}">
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding Parent.IsTextMode}"
                                 Value="True">
                      <Setter Property="Cursor"
                              Value="IBeam" />
                    </DataTrigger>
                  </Style.Triggers>
                </Style>
              </Grid.Style>
              <ContentPresenter Width="{Binding Parent.PageWidth}"
                                Height="{Binding Parent.PageHeight}"
                                Content="{Binding WrappedVisual}" />
              <ItemsControl x:Name="PastScribbles"
                            ItemsSource="{Binding ScribbleData}">
                <ItemsControl.ItemsPanel>
                  <ItemsPanelTemplate>
                    <Grid />
                  </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                  <DataTemplate>
                    <Grid Width="{Binding DataContext.PageWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"
                          Height="{Binding DataContext.PageHeight, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"
                          Visibility="{Binding IsVisible, Converter={StaticResource BoolToVisibilityConverter}}">
                      <InkPresenter Strokes="{Binding Strokes}"
                                    Visibility="{Binding InkVisibility}">
                        <InkPresenter.Effect>
                          <DropShadowEffect Opacity="{Binding DropShadowOpacity}"
                                            ShadowDepth="2" />
                        </InkPresenter.Effect>
                      </InkPresenter>

                      <ItemsControl Width="{Binding DataContext.PageWidth, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"
                                    Height="{Binding DataContext.PageHeight, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}}"
                                    ItemsSource="{Binding TextData}"
                                    Visibility="{Binding TextVisibility}">
                        <ItemsControl.ItemsPanel>
                          <ItemsPanelTemplate>
                            <Canvas />
                          </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                          <DataTemplate>
                            <TextBlock Margin="4"
                                       FontSize="{Binding TextData.FontSize}"
                                       FontWeight="{Binding FontWeight}"
                                       Text="{Binding TextData.Text, FallbackValue=This is a placeholder}"
                                       TextWrapping="Wrap"
                                       MaxWidth="{Binding TextData.MaxWidth}">
                              <TextBlock.Foreground>
                                <SolidColorBrush Color="{Binding TextData.TextColor, FallbackValue=Black}" />
                              </TextBlock.Foreground>
                            </TextBlock>
                          </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.ItemContainerStyle>
                          <Style>
                            <Setter Property="Canvas.Left"
                                    Value="{Binding TextData.LeftPosition}" />
                            <Setter Property="Canvas.Top"
                                    Value="{Binding TextData.TopPosition}" />
                          </Style>
                        </ItemsControl.ItemContainerStyle>
                        <ItemsControl.Effect>
                          <DropShadowEffect Opacity="{Binding DropShadowOpacity}"
                                            ShadowDepth="2" />
                        </ItemsControl.Effect>
                      </ItemsControl>

                    </Grid>
                  </DataTemplate>
                </ItemsControl.ItemTemplate>
              </ItemsControl>
            </Grid>
          </InkCanvas>
        </Border>

ScribblePad.cs

  /// <summary>
  /// Interaction logic for ScribblePadUC.xaml
  /// </summary>
  public partial class ScribblePadUC : UserControl
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="ScribblePadUC"/> class.
    /// </summary>
    public ScribblePadUC()
    {
      this.InitializeComponent();
    }

    /// <summary>
    /// Handles the RequestBringIntoView event of the Border control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.RequestBringIntoViewEventArgs"/> instance containing the event data.</param>
    private void Border_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
    {
      // Don't want auto-scrolling when trying to scribble... causes annoying vertical line
      e.Handled = true;
    }

    /// <summary>
    /// Handles the Loaded event of the TextBox control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
    private void TextBox_Loaded(object sender, RoutedEventArgs e)
    {
      ((TextBox)sender).Focus();
    }

    /// <summary>
    /// Event handler for the PreviewKeyDown event
    /// </summary>
    /// <param name="sender">The sender of the event</param>
    /// <param name="e">The event arguments</param>
    private void UserControl_PreviewKeyDown(object sender, KeyEventArgs e)
    {
      bool openAddendem = false;

      if (Keyboard.IsKeyDown(Key.A) && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
      {
        openAddendem = true;
      }

      if (openAddendem == true)
      {
        ScribblePadVM vm = this.DataContext as ScribblePadVM;

        if (vm != null)
        {
          if (vm.AddendumWidth.Value == 0)
          {
            vm.AddendumOpenCloseCommand.Execute(null);
          }

          vm.AddAddendumCommand.Execute(true);

          e.Handled = true;
        }
      }
    }

    /// <summary>
    /// Event handler for the loaded event
    /// </summary>
    /// <param name="sender">The sender of the event</param>
    /// <param name="e">The event arguments</param>
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      Window window = this.TryFindVisualParent<Window>();

      if (window != null && window is HealthRecord.HealthRecordWindow)
      {
        window.PreviewKeyDown += (s, args) =>
        {
          this.UserControl_PreviewKeyDown(s, args);
        };
      }
    }
  }
}

我故意将 ViewModel.cs 排除在外,因为它很可能与此问题无关。

我想要发生的事情

当我使用触摸屏时,如果我尝试在画布上绘图,则无法控制滚动条。

感谢阅读。

【问题讨论】:

  • 我假设您也使用 PreviewTouchDown 进行了测试?
  • 不,这就是答案.. 太简单了!谢谢。

标签: c# wpf xaml wpf-controls inkcanvas


【解决方案1】:

PreviewTouchDown 允许您处理触摸事件之前它冒泡到相应的 UI 元素。 PreviewMouseDown 也是如此。

(给出官方答案,以便问题标记为已解决)

【讨论】:

    猜你喜欢
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多