向下滚动时,ScrollViewer中的图形覆盖ScrollViewer外部的图形(WPF)

时间:2014-01-15 09:52:27

标签: wpf xaml drawing

我有一个XAML文件。在这个XAML文件中,我有一个ScrollViewer,稍后我会添加WinFormsHost - >基本上放在标准WinForm的位置。当我向下滚动ScrollViewer时,图形从里面向外移动。请看第一个第二个截图。 如何处理绘图问题?我可以为ScrollViewer设置ZIndex吗?

<Window x:Class="Example.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Example" 
        Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded"

        >

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="3*" />
            <RowDefinition Height="7*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" /> <!-- this column is for future purpouses -->
            <ColumnDefinition Width="1366" />
        </Grid.ColumnDefinitions>
        <Grid Grid.Row="1" Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="3*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <Border BorderBrush="Red" BorderThickness="4" Grid.Column="0" Grid.Row="0">
                <ListBox x:Name="ListBox"   
                </ListBox>
            </Border>
            <Button x:Name="AcceptButton" Grid.Row="1" ></Button>
        </Grid>  
        <Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
            <ScrollViewer Grid.Column="1" Grid.Row="2" >
                <WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
                </WrapPanel>
            </ScrollViewer>
        </Border>   
    </Grid>   
</Window>

开始

enter image description here


向下滚动 - 当所有内容都恢复正常时

enter image description here

**编辑**试过,但没有区别:

<Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2" ClipToBounds="True">
            <ScrollViewer ClipToBounds="True" Grid.Column="1" Grid.Row="2" >
                <WrapPanel ClipToBounds="True" x:Name="VideoPanel" Grid.Column="1" Grid.Row="2">
                </WrapPanel>
            </ScrollViewer>
        </Border>

编辑2:

XAML

<Window x:Class="HomeSecurity.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:HomeSecurity" 
        Title="MainWindow" WindowState="Maximized" Loaded="Window_Loaded">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="9*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="8*" />
        </Grid.ColumnDefinitions>
        <Border BorderBrush="Green" BorderThickness="2" Grid.Column="1" Grid.Row="2">
            <ScrollViewer >
                <WrapPanel x:Name="VideoPanel" >
                </WrapPanel>
            </ScrollViewer>
        </Border>
        <Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="23,30,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>

</Window>

MAINWINDOW.XAML.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HomeSecurity {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e) {
            add(); add(); add();
        }

        private void add() {


            WindowsFormsHost formsHost = new WindowsFormsHost();
            VideoStream videoStream = new VideoStream();
            formsHost.Width = 400;
            formsHost.Height = 400;
            formsHost.Child = videoStream;
            Border lineBorder = new Border();
            lineBorder.BorderBrush = Brushes.Green;
            lineBorder.BorderThickness = new Thickness(2);
            lineBorder.Child = formsHost;
            VideoPanel.Children.Add(lineBorder);
        }
    }
}

必须添加对System.WindowsFormsIntegrationSystem.Windows.FormsSystem.Drawing的引用。

1 个答案:

答案 0 :(得分:0)

你显然有一些问题,你应该彻底调查你的代码发生了什么,以试图最佳地纠正这种情况。但是,您在此处显示的代码不应该受到指责。我在一个新项目中尝试了它,它按预期工作。由于WrapPanel内没有任何内容,ScrollViewer中没有任何内容可以解决您的问题。在添加代码以演示问题时,您应确保代码 演示问题。

即便如此,如果无法重新创建问题,可能只是一个快速修复,您可以在这种情况下使用。您可以将Border上的UIElement.ClipToBounds property设置为True,这将剪切超出其范围的任何内容。

<Border ClipToBounds="True" BorderBrush="Green" BorderThickness="2" ...>
    <ScrollViewer Grid.Column="1" Grid.Row="2" >
        <WrapPanel x:Name="WinFormsPanel" Grid.Column="1" Grid.Row="2">
        </WrapPanel>
    </ScrollViewer>
</Border>