Scrollviewer, canvas et apparition des scrollbar
Bonjour,
J'ai un canvas dans un scroll viewer :
Code:
1 2 3 4 5 6
|
<ScrollViewer Name="theScrollViewer" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" Grid.Row="1" CanContentScroll="True">
<DockPanel>
<Canvas Name="theStack" ClipToBounds="True"/>
</DockPanel>
</ScrollViewer> |
Je remplis le canvas via code, mais malheureusement la scrollbar du canvas n'apparait pas automatiquement comme pour les autres conteneurs.
Merci.
canvas avec scrollbar et zoom
bonjour
ca depend si tu as personnalise ta classe canvas en surchargeant la methode MeasureOverride qui doit faire une espece de mise à l'echelle.
ici code behind de la classe canvas perso :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
Imports System
Imports System.Windows
Imports System.Windows.Controls
Public Class CanvasScrollable
Inherits Canvas
Public Sub New()
'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
'This style is defined in Themes\Generic.xaml
DefaultStyleKeyProperty.OverrideMetadata(GetType(CanvasScrollable), New FrameworkPropertyMetadata(GetType(CanvasScrollable)))
End Sub
'Surcharge overrides MeasureOverride
Protected Overrides Function MeasureOverride(ByVal constraint As System.Windows.Size) As System.Windows.Size
'Return MyBase.MeasureOverride(constraint)
Dim bottomMost As Double = 0.0
Dim rightMost As Double = 0.0
'Boucle sur tous les elements enfanst et
'recherche plus grande valeur de Top and Left parmi eux.
For Each obj As Object In Me.Children
Dim x_child As FrameworkElement = TryCast(obj, FrameworkElement)
If (x_child IsNot Nothing) Then
x_child.Measure(constraint)
bottomMost = Math.Max( _
bottomMost, GetTop(x_child) + x_child.DesiredSize.Height)
rightMost = Math.Max( _
rightMost, GetLeft(x_child) + x_child.DesiredSize.Width)
End If
Next
If (Double.IsNaN(bottomMost) Or Double.IsInfinity(bottomMost)) Then
bottomMost = 0.0
End If
If (Double.IsNaN(rightMost) Or Double.IsInfinity(rightMost)) Then
rightMost = 0.0
End If
'Renvoi nouvelle taille
Return New Size(rightMost, bottomMost)
End Function
End Class |
ici code de la forme qui utilise ce canvas personnalise scrollable:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestCanvas"
Title="WPF Scrollable Canvas" Height="Auto" Width="Auto" SizeToContent="Manual" >
<DockPanel >
<ScrollViewer
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto" >
<local:CanvasScrollable
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Beige" >
<Rectangle
Canvas.Top="0"
Canvas.Left="0"
StrokeThickness="2"
Stroke="DarkRed"
Width="50"
Height="50"
/>
<Rectangle
Canvas.Top="150"
Canvas.Left="150"
StrokeThickness="2"
Stroke="Blue"
Width="150"
Height="150"
/>
<Rectangle
Canvas.Top="200"
Canvas.Left="200"
StrokeThickness="2"
Stroke="Green"
Width="200"
Height="200"
/>
</local:CanvasScrollable>
</ScrollViewer>
</DockPanel>
</Window> |
ici code d'une 2eme forme qui utilise la -meme-classe canvas personnalise sans modif- et on a rajoute simplement un slider dans Window2 pour faire le zoom:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
<Window x:Class="Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestCanvas"
Title="WPF Scrollable et Zoomable Canvas" Height="Auto" Width="Auto" SizeToContent="Manual" >
<DockPanel >
<Slider
DockPanel.Dock="Bottom"
x:Name="zoomSlider"
Minimum="0.1"
Maximum="5"
Value="1" Background="CadetBlue" />
<ScrollViewer
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto" >
<local:CanvasScrollable
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Beige" >
<Canvas.LayoutTransform>
<ScaleTransform
ScaleX="{Binding Path=Value, ElementName=zoomSlider}"
ScaleY="{Binding Path=Value, ElementName=zoomSlider}"
/>
</Canvas.LayoutTransform>
<Rectangle
Canvas.Top="0"
Canvas.Left="0"
StrokeThickness="2"
Stroke="Red"
Fill="Blue"
Width="50"
Height="50"
/>
<Rectangle
Canvas.Top="150"
Canvas.Left="150"
StrokeThickness="2"
Stroke="Blue"
Fill="LimeGreen"
Width="150"
Height="150"
/>
<Rectangle
Canvas.Top="200"
Canvas.Left="200"
StrokeThickness="2"
Stroke="Green"
Width="200"
Height="200"
/>
</local:CanvasScrollable>
</ScrollViewer>
</DockPanel>
</Window> |
bon code.....................