IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Scrollviewer, canvas et apparition des scrollbar


Sujet :

Windows Presentation Foundation

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 537
    Points : 369
    Points
    369
    Par défaut Scrollviewer, canvas et apparition des scrollbar
    Bonjour,

    J'ai un canvas dans un scroll viewer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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.

  2. #2
    Membre régulier
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    69
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 69
    Points : 73
    Points
    73
    Par défaut
    Ca doit être parce que ton Canvas n'a ni hauteur, ni largeur. Si tu crées un Canvas de 50x50 par exemple, tu peux dessiner en dehors, sans que ça ne pose problème, le Canvas ne sera pas agrandit pour autant (du coup là ça pose problème...).

    Tu peux faire des tests en mettant une couleur de fond sur ton Canvas.

  3. #3
    Expert confirmé
    Inscrit en
    Avril 2008
    Messages
    2 564
    Détails du profil
    Informations personnelles :
    Âge : 64

    Informations forums :
    Inscription : Avril 2008
    Messages : 2 564
    Points : 4 441
    Points
    4 441
    Par défaut 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 vb.net : Sélectionner tout - Visualiser dans une fenêtre à part
    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 xml : Sélectionner tout - Visualiser dans une fenêtre à part
    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 xml : Sélectionner tout - Visualiser dans une fenêtre à part
    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.....................

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 537
    Points : 369
    Points
    369
    Par défaut
    Ok merci beaucoup pour l'aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Utilisation des ScrollBar avec un ScrollView
    Par vanitom dans le forum MFC
    Réponses: 3
    Dernier message: 11/04/2007, 10h45
  2. [ eclipse ][ view ] ajouter des scrollbars
    Par Jean-Philippe Shields dans le forum Eclipse Java
    Réponses: 3
    Dernier message: 02/12/2005, 16h38
  3. Colorer des scrollbar d iframe
    Par NeHuS dans le forum Balisage (X)HTML et validation W3C
    Réponses: 3
    Dernier message: 04/08/2005, 17h32
  4. [Servlet] Logging sans apparition des informations
    Par hedgehog dans le forum Servlets/JSP
    Réponses: 6
    Dernier message: 12/05/2005, 14h26
  5. Epaisseur des scrollbar !
    Par Gab-Gab dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 13/11/2004, 23h02

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo