Question

I'm using a ScrollViewer in a WPF project and i'm struggling with its content. The window this SV is contained in has many other UI items and I'd like to make the Image scrolled by the ScrollViewer not visible out of the SV zone or at least BEHIND other elements.

This is my bit of code with the SV (yes, it's inside a grid):

<Grid Name="mainGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="200*" />
        <ColumnDefinition Width="802*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="30*" />
        <RowDefinition Height="500*" />
        <RowDefinition Height="199*" />
    </Grid.RowDefinitions>

    <ScrollViewer Grid.Row="1" Grid.ColumnSpan="2" Name="map" Margin="0,0,0,0" PanningMode="Both" HorizontalScrollBarVisibility="Visible" Background="DarkGray" ClipToBounds="True">
        <WindowsFormsHost Name="windowsFormsHost1" Cursor="Cross" HorizontalAlignment="Left" VerticalAlignment="Top" ClipToBounds="True" />
    </ScrollViewer>
</grid>

Another issue I'm having, I'd like to Scroll the image while having my mouse on it. as it is, it only scrolls when the mouse is on an empty aera of the ScrollViewer.

This is a part of the behind code :

public MainWindow()
{
    InitializeComponent();

    //Creation of the map
    Map newMap = new Map();
    newMap.setMapStrategy(new SmallMapStrategy());
    newMap.createMap();

    //Put the map in the PB as an image
    System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
    pictureBox.Width = newMap.grid.Count * 2; pictureBox.Height = newMap.grid.Count * 2;
    newMap.afficher(pictureBox);
    windowsFormsHost1.Width = newMap.grid.Count * 2; windowsFormsHost1.Height = newMap.grid.Count * 2;
    windowsFormsHost1.Child = pictureBox;
}
Was it helpful?

Solution

I solved my problem using ScrollableControl. so now, my PictureBox is in a scrollable control that is in a WindowsFormsHost contained into a Grid.

        System.Windows.Forms.PictureBox pictureBox = new System.Windows.Forms.PictureBox();
        pictureBox.Width = (int)Math.Sqrt((double)game.Map.grid.Count) * 50; pictureBox.Height = (int)Math.Sqrt((double)game.Map.grid.Count) * 50;
        game.Map.afficher(pictureBox);
        System.Windows.Forms.ScrollableControl sc = new System.Windows.Forms.ScrollableControl();
        sc.Controls.Add(pictureBox);
        sc.AutoScroll = true;
        windowsFormsHost1.Child = sc;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top