Create a Scrollable Canvas Control : Canvas « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Windows Presentation Foundation » CanvasScreenshots 
Create a Scrollable Canvas Control
Create a Scrollable Canvas Control
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="200" Width="400" >

    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="Auto" />
            <Setter Property="Height" Value="24" />
        </Style>
    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.5*"/>
            <ColumnDefinition Width="0.5*"/>
        </Grid.ColumnDefinitions>

        <ScrollViewer Grid.Column="0">
            <Canvas>
                <Button Canvas.Top="80" Canvas.Left="80">In View</Button>
                <Button Canvas.Top="300" Canvas.Left="80">Out of view</Button>
            </Canvas>
        </ScrollViewer>

        <ScrollViewer Grid.Column="1">
            <WpfApplication1:ScrollableCanvasControl>
                <Button Canvas.Top="80" Canvas.Left="80">In View</Button>
                <Button Canvas.Top="300" Canvas.Left="80">Out of View</Button>
            </WpfApplication1:ScrollableCanvasControl>
        </ScrollViewer>

    </Grid>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public class ScrollableCanvasControl : Canvas
    {
        static ScrollableCanvasControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollableCanvasControl)new FrameworkPropertyMetadata(typeof(ScrollableCanvasControl)));
        }

        protected override Size MeasureOverride(Size constraint)
        {
            double bottomMost = 0d;
            double rightMost = 0d;

            foreach(object obj in Children)
            {
                FrameworkElement child = obj as FrameworkElement;

                if(child != null)
                {
                    child.Measure(constraint);

                    bottomMost = Math.Max(bottomMost,GetTop(child+child.DesiredSize.Height);
                    rightMost = Math.Max(rightMost, GetLeft(child+ child.DesiredSize.Width);
                }
            }
            return new Size(rightMost, bottomMost);
        }
    }
}

   
    
  
Related examples in the same category
1.Nested CanvasNested Canvas
2.Positioning on a CanvasPositioning on a Canvas
3.Canvas without ViewboxCanvas without Viewbox
4.Animation target Canvas Left, TopAnimation target Canvas Left, Top
5.Pixel Not SnappedPixel Not Snapped
6.Rotate ShapeRotate Shape
7.Canvas dependantCanvas dependant
8.Simple Canvas LayoutSimple Canvas Layout
9.Canvas.SetTopCanvas.SetTop
10.Canvas.SetBottomCanvas.SetBottom
11.Canvas.SetRightCanvas.SetRight
12.Canvas PreviewMouseDown action and MouseDown actionCanvas PreviewMouseDown action and MouseDown action
13.Create a Zoomable Canvas ControlCreate a Zoomable Canvas Control
14.Set control position for CanvasSet control position for Canvas
15.Canvas Positioning Properties SampleCanvas Positioning Properties Sample
16.Get position on a Canvas with Canvas.GetLeftGet position on a Canvas with Canvas.GetLeft
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.