如何隐藏ScrollViewer之外的内容?

时间:2013-01-04 13:21:10

标签: c# wpf xaml scrollviewer windowsformshost

我在WPF项目中使用ScrollViewer,我正在努力解决其内容问题。 包含此SV的窗口包含许多其他UI项目,我想使ScrollViewer滚动的图像在SV区域中不可见或至少在其他元素后面。

这是我对SV的代码(是的,它在网格内):

<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>

我遇到的另一个问题是,我想在将鼠标放在上面的同时滚动图像。 实际上,它仅在鼠标位于ScrollViewer的空白区域时滚动。

这是后面代码的一部分:

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

1 个答案:

答案 0 :(得分:1)

我使用ScrollableControl解决了我的问题。 所以现在,我的PictureBox处于一个可滚动的控件中,该控件位于包含在Grid中的WindowsFormsHost中。

        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;