单击、拖动和滚动画布视图

发布于 2024-11-14 19:44:11 字数 1687 浏览 9 评论 0原文

我有一个带有 Canvas ItemsPanelTemplate 的列表框。我知道 ScrollViewer 不能与 Canvas 一起使用,除非给定高度和宽度。我不想给画布指定高度和宽度,因为它并不总是恒定的。是否还有任何其他解决方法或技巧可以解决这种情况。我知道我不是唯一一个遇到这个问题的人。预先感谢这是我到目前为止的代码。

另一个问题是我无法将 ScrollViewer 放置在 ItemsPanelTemplate 中,因为它只能嵌套一个元素。

这也限制了我将画布放置在网格内以获得定位。

XAML:

    <!--Core Viewer-->
    <ScrollViewer x:Name="scrollViewer"
                  VerticalScrollBarVisibility="Hidden"
                  HorizontalScrollBarVisibility="Hidden">

        <ListBox x:Name="objCoreViewer"
             ItemsSource="{Binding ItemsSource}"
             Background="LightGray"
             SelectionChanged="objCoreViewer_SelectionChanged"
             ItemTemplateSelector="{DynamicResource CoreViewerDataTemplateSelector}"
             ItemContainerStyleSelector="{DynamicResource ItemContainerStyleSelector}"
             PreviewMouseWheel="objCoreViewer_PreviewMouseWheel">

            <!-- Core Map Canvas -->

            <ListBox.ItemsPanel>

                <ItemsPanelTemplate>
                    <Canvas x:Name="objCoreViewerCanvas"
                            Background="Transparent">
                        <Canvas.LayoutTransform>
                            <ScaleTransform ScaleX="{Binding Path=Value, ElementName=ZoomSlider}"
                                            ScaleY="{Binding Path=Value, ElementName=ZoomSlider}" />
                        </Canvas.LayoutTransform>
                    </Canvas>
                </ItemsPanelTemplate>

            </ListBox.ItemsPanel>

        </ListBox>

    </ScrollViewer>

I have a ListBox with a ItemsPanelTemplate of Canvas. I know the ScrollViewer will not work with the Canvas unless its given a height and width. I DO NOT want to give the canvas a height and width because it will not always be constant. Is there any other work around or tricks anyone has gotten to work for this situation. I know I can't be the only one with this problem. Thanks in advance here is my code so far.

Another problem is I am unable to place the ScrollViewer inside the ItemsPanelTemplate because it can only have one element nested inside.

This also restricts me from placing the canvas inside a grid to get positioning.

XAML:

    <!--Core Viewer-->
    <ScrollViewer x:Name="scrollViewer"
                  VerticalScrollBarVisibility="Hidden"
                  HorizontalScrollBarVisibility="Hidden">

        <ListBox x:Name="objCoreViewer"
             ItemsSource="{Binding ItemsSource}"
             Background="LightGray"
             SelectionChanged="objCoreViewer_SelectionChanged"
             ItemTemplateSelector="{DynamicResource CoreViewerDataTemplateSelector}"
             ItemContainerStyleSelector="{DynamicResource ItemContainerStyleSelector}"
             PreviewMouseWheel="objCoreViewer_PreviewMouseWheel">

            <!-- Core Map Canvas -->

            <ListBox.ItemsPanel>

                <ItemsPanelTemplate>
                    <Canvas x:Name="objCoreViewerCanvas"
                            Background="Transparent">
                        <Canvas.LayoutTransform>
                            <ScaleTransform ScaleX="{Binding Path=Value, ElementName=ZoomSlider}"
                                            ScaleY="{Binding Path=Value, ElementName=ZoomSlider}" />
                        </Canvas.LayoutTransform>
                    </Canvas>
                </ItemsPanelTemplate>

            </ListBox.ItemsPanel>

        </ListBox>

    </ScrollViewer>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2024-11-21 19:44:11

要使画布根据其内部的子元素增长,您需要在继承自 Canavas 的自定义类中重写 Canvas 的 MeasureOverride 事件。这段代码对我来说非常有用:

 protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
    {
        Size toReport = new Size();

        foreach (UIElement element in this.InternalChildren)
        {
            //Get the left most and top most point.  No using Bottom or Right in case the controls actual bottom and right most points are less then the desired height/width
            var left = Canvas.GetLeft(element);
            var top = Canvas.GetTop(element);

            left = double.IsNaN(left) ? 0 : left;
            top = double.IsNaN(top) ? 0 : top;

            element.Measure(constraint);

            Size desiredSize = element.DesiredSize;

            if (!double.IsNaN(desiredSize.Width) && !double.IsNaN(desiredSize.Height))
            {
                //left += desiredSize.Width;
                //top += desiredSize.Height;

                toReport.Width = toReport.Width > left +desiredSize.Width ? toReport.Width : left + desiredSize.Width;
                toReport.Height = toReport.Height > top+desiredSize.Height ? toReport.Height : top + desiredSize.Height;
            }

        }

        //Make sure scroll includes the margins incase of a border or something
        toReport.Width += this.Margin.Right;
        toReport.Height += this.Margin.Bottom;

        return toReport;
    }

To get the canvas to grow based on the the child elements inside of it you will need to override the Canvas's MeasureOverride event in a custom class that inherits from Canavas. This code works great for me:

 protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint)
    {
        Size toReport = new Size();

        foreach (UIElement element in this.InternalChildren)
        {
            //Get the left most and top most point.  No using Bottom or Right in case the controls actual bottom and right most points are less then the desired height/width
            var left = Canvas.GetLeft(element);
            var top = Canvas.GetTop(element);

            left = double.IsNaN(left) ? 0 : left;
            top = double.IsNaN(top) ? 0 : top;

            element.Measure(constraint);

            Size desiredSize = element.DesiredSize;

            if (!double.IsNaN(desiredSize.Width) && !double.IsNaN(desiredSize.Height))
            {
                //left += desiredSize.Width;
                //top += desiredSize.Height;

                toReport.Width = toReport.Width > left +desiredSize.Width ? toReport.Width : left + desiredSize.Width;
                toReport.Height = toReport.Height > top+desiredSize.Height ? toReport.Height : top + desiredSize.Height;
            }

        }

        //Make sure scroll includes the margins incase of a border or something
        toReport.Width += this.Margin.Right;
        toReport.Height += this.Margin.Bottom;

        return toReport;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文