ItemsControl-> MousePress在其他实例上的多个InkCanvas实例导致行长

在ItemsControl中使用多个InkCanvas实例时遇到问题。每个InkCanvas实例接收一个ImageBrush作为背景,然后将其添加到ItemsControl。基本上,这是一个自写的PdfViewer,用户可以在PdfPages上绘制备注等,然后再次将文件另存为pdf。阅读原始的pdf文件,将页面转换为位图,并将每个位图用作InkCanvas的背景。

只要我不单击其他画布,此效果就很好。这是两个图像来说明问题:

ItemsControl-> MousePress在其他实例上的多个InkCanvas实例导致行长

ItemsControl-> MousePress在其他实例上的多个InkCanvas实例导致行长

PdfViewer的XAML:

<UserControl x:Class="PdfTool.Code.Controls.PdfViewer"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:viewModel="clr-namespace:PdfTool.Code.ViewModel"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance {x:Type viewModel:CanvasViewModel},IsDesignTimeCreatable=False}"
             d:DesignHeight="450" d:DesignWidth="800">
    <ScrollViewer PanningMode="Both" Background="{StaticResource MainBackgroundColor}">
        <ItemsControl x:Name="PagesContainer" MouseWheel="PagesContainer_OnmouseWheel">
            <ItemsControl.RenderTransform>
                <MatrixTransform/>
            </ItemsControl.RenderTransform>
        </ItemsControl>
    </ScrollViewer>
</UserControl>

后面的代码:

public partial class PdfViewer : UserControl
{
    #region Bindable Properties
    ...
    #endregion

    public PdfViewer()
    {
        InitializeComponent();
    }

    private static async Task PdfToImages(PdfViewer pdfViewer,PdfDocument pdfDoc)
    {
        pdfViewer.Dispatcher.Invoke(() => pdfViewer.PagesContainer.Items.Clear());
        if (pdfDoc == null) return;

        for (uint i = 0; i < pdfDoc.PageCount; i++) {
            using (var page = pdfDoc.GetPage(i)) {
                await pdfViewer.Dispatcher.Invoke(async () =>
                {
                    var bitmap = await PageToBitmapAsync(page);
                    pdfViewer.PagesContainer.Items.Add(new PdfDetailElement(new ImageBrush(bitmap) {Stretch = Stretch.Uniform}));
                });
            }
        }
    }

    ...
}

单个PdfPage的XAML:

<UserControl x:Class="PdfTool.Code.Controls.PdfDetailElement"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:viewModel="clr-namespace:PdfTool.Code.ViewModel"
             mc:Ignorable="d" 
             d:DataContext="{d:DesignInstance {x:Type viewModel:CanvasViewModel},IsDesignTimeCreatable=False}"
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid Margin="10,10,5,10">
        <Viewbox Stretch="Fill">
            <InkCanvas x:Name="inkCanvas" 
                       Background="{x:Null}" 
                       DefaultDrawingAttributes="{Binding SelectedDrawingAttributes}" 
                       EditingMode="{Binding SelectedEditingMode,UpdateSourceTrigger=PropertyChanged}"/>
        </Viewbox>
    </Grid>
</UserControl>

后面的代码:

public partial class PdfDetailElement : UserControl
{
    public PdfDetailElement()
    {
        InitializeComponent();
    }

    public PdfDetailElement(ImageBrush image)
    {
        DataContext = Gui.Main.CanvasSettings;
        InitializeComponent();
        inkCanvas.Background = image;
        inkCanvas.Width = image.ImageSource.Width;
        inkCanvas.Height = image.ImageSource.Height;
    }
}

仅MouseWheel事件用于“缩放”,它不是问题的原因。看来,InkCanvas 1上的鼠标坐标用作InkCanvas 2的初始值。如何更改此行为?

jeseca8897 回答:ItemsControl-> MousePress在其他实例上的多个InkCanvas实例导致行长

我明白了:Stop WPF ScrollViewer automatically scrolling to perceived content使我朝着正确的方向前进。我只需要设置

Focusable="False"

在InkCanvas上解决了我的问题,因为如果要显示的对象无法聚焦,则不会触发滚动条的RequestBringIntoView事件...

本文链接:https://www.f2er.com/2538372.html

大家都在问