Dragging, dragging and scrolling canvas

I have a ListBox with the ItemsPanelTemplate of the canvas. I know that ScrollViewer will not work with Canvas if it is not given height and width. I DO NOT want the canvas to have height and width, because it will not always be permanent. Is there any other job or tricks that someone got to work in this situation. I know that I cannot be the only one with this problem. Thanks in advance, this is my code.

Another problem is that I cannot put the ScrollViewer inside the ItemsPanelTemplate, because it can only contain one item inside.

It also prevents me from placing the canvas inside the 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>
+3
source share
1 answer

, Canvas MeasureOverride , Canavas. :

 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;
    }
+2

All Articles