阻止 WPF InkBrush 引起滚动事件
Stop WPF InkBrush from causing a scroll event
应用程序
我正在处理 EHR 应用程序。我正在处理的页面允许用户使用 InkCanvas
和 InkBrush
绘制患者的笔记。当用户使用鼠标绘图时,一切都完美无缺。 但是,当他们使用触摸屏绘制滚动条时也是被操纵的。这会导致页面在绘制时滚动。
我试过的
到目前为止,我主要使用e.handled = true
.
尝试处理TouchDown
、TouchUp
和TouchMove
等触摸事件
应用程序代码(抱歉,代码太多!)
ScribblePad.xaml(Border
+ InkCanvas
代码片段)
<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 排除在外,因为它很可能与此问题无关。
我想要发生的事情
当我使用触摸屏时,如果我试图在 canvas 上绘图,则滚动条不受控制。
感谢阅读。
PreviewTouchDown allows you to handle the touch event before it bubbles to the corresponding UI Element. The same goes for PreviewMouseDown.
(放上官方答案,这样问题就可以标记为已解决)
应用程序
我正在处理 EHR 应用程序。我正在处理的页面允许用户使用 InkCanvas
和 InkBrush
绘制患者的笔记。当用户使用鼠标绘图时,一切都完美无缺。 但是,当他们使用触摸屏绘制滚动条时也是被操纵的。这会导致页面在绘制时滚动。
我试过的
到目前为止,我主要使用e.handled = true
.
TouchDown
、TouchUp
和TouchMove
等触摸事件
应用程序代码(抱歉,代码太多!)
ScribblePad.xaml(Border
+ InkCanvas
代码片段)
<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 排除在外,因为它很可能与此问题无关。
我想要发生的事情
当我使用触摸屏时,如果我试图在 canvas 上绘图,则滚动条不受控制。
感谢阅读。
PreviewTouchDown allows you to handle the touch event before it bubbles to the corresponding UI Element. The same goes for PreviewMouseDown.
(放上官方答案,这样问题就可以标记为已解决)