0

我对 GridSplitter 的可见性有疑问。

在此,无论我托管 Winform DataGridView。拖动时,GridSplitter 在其他控件上正确可见。但不在这个网格上。事实上,无论我托管什么而不是 Datagridview,都会成为最顶层的控件,这使得 GridSplitter 隐藏在它后面。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Name="rowForButton"/>
        <RowDefinition Name="rowForGridSplitter" Height="Auto" MinHeight="81" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Height="50" Width="110" Content="Button in First Row"/>
    <my:WindowsFormsHost Panel.ZIndex="0" Grid.Row="1"  Margin="30,11,138,0" x:Name="winHost" Height="58" VerticalAlignment="Top" OpacityMask="Transparent">            
        <win:DataGridView x:Name="dataGridView"></win:DataGridView>
    </my:WindowsFormsHost>        
    <GridSplitter  BorderThickness="1" Panel.ZIndex="1" Grid.Row="1" HorizontalAlignment="Stretch" Height="5" ShowsPreview="True" VerticalAlignment="Top">
    </GridSplitter>
</Grid>
4

6 回答 6

1

通常,您应该将 GridSplitter 放入其自己的网格单元中,或者通过边距确保没有控件可以与其重叠。但我不知道这是否完全适用于你。另请参见此处

于 2009-04-06T06:44:25.930 回答
1

我也遇到了这个问题,有我的解决方法:

var splitter = new GridSplitter()
        {
            HorizontalAlignment = HorizontalAlignment.Stretch,
            VerticalAlignment = VerticalAlignment.Stretch,
            FocusVisualStyle = null,
            ShowsPreview = true,
            Background = new SolidColorBrush(new Color() { R = 1, G = 1, B = 1, A = 1 }),
        };

// non-style / essential window which will display over your WinForm control
var PopupWindowForSplitter = new PopupWindow()
        {
            Background = new SolidColorBrush(new Color() { R = 1, G = 1, B = 1, A = 1 }),
            Visibility = Visibility.Collapsed
        };
PopupWindowForSplitter.Show();
...

Point _ptForSplitterDrag = new Point(0,0);

splitter.DragStarted += (o, e) =>
        {
            var pt = splitter.PointToScreen(new Point());
            _ptForSplitterDrag = splitter.PointToScreen(Mouse.GetPosition(splitter));
            PopupWindowForSplitter.Left = pt.X;
            PopupWindowForSplitter.Top = pt.Y;
            PopupWindowForSplitter.Height = splitter.ActualHeight;
            PopupWindowForSplitter.Width = splitter.ActualWidth;
            PopupWindowForSplitter.Activate();
            PopupWindowForSplitter.Visibility = Visibility.Visible;
        };
        splitter.DragDelta += (o, e) =>
        {
            var pt = splitter.PointToScreen(Mouse.GetPosition(splitter)) - _ptForSplitterDrag
                + splitter.PointToScreen(new Point());
            if (splitter.ResizeDirection == GridResizeDirection.Rows)
            {
                PopupWindowForSplitter.Top = pt.Y;
            }
            else
            {
                PopupWindowForSplitter.Left = pt.X;
            }
        };
        splitter.DragCompleted += (o, e) =>
        {
            var initializeData = typeof(GridSplitter).GetMethod("InitializeData", BindingFlags.NonPublic | BindingFlags.Instance);
            var moveSplitter = typeof(GridSplitter).GetMethod("MoveSplitter", BindingFlags.NonPublic | BindingFlags.Instance);
            if (moveSplitter != null && initializeData != null)
            {
                initializeData.Invoke(splitter, new object[] { true });

                var pt = splitter.PointToScreen(Mouse.GetPosition(splitter)) - _ptForSplitterDrag;
                if (splitter.ResizeDirection == GridResizeDirection.Rows)
                {
                    moveSplitter.Invoke(splitter, new object[] { 0, pt.Y });
                }
                else
                {
                    moveSplitter.Invoke(splitter, new object[] { pt.X, 0 });
                }
            }
            PopupWindowForSplitter.Visibility = Visibility.Collapsed;
        };

也许因为我的英语不好,我的描述中有一些问题,但我认为代码足以解释它。

于 2012-05-18T07:42:34.223 回答
0

尝试使用 WPF 本机 DataGrid 控件。您可以购买几个商业第三方控件,或者您可以查看 Microsoft 提供的一个(目前仍在 CTP 中):

于 2009-04-06T08:42:35.410 回答
0

Windows 窗体控件始终与您的 WPF 控件分开呈现,因此将始终显示在您的 WPF 应用程序上。

有关详细信息,请参阅在 WPF 中托管 Microsoft Win32 窗口(副标题Notable Differences in Output Behavior)。

于 2009-04-06T12:24:38.390 回答
0

在您的情况下,最快的解决方法是将 GirdSplitter 移动到带有按钮的行:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Name="rowForButton"/>
        <RowDefinition Name="rowForGridSplitter" Height="Auto" MinHeight="81" />
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Height="50" Width="110" Content="Button in First Row"/>
    <my:WindowsFormsHost Panel.ZIndex="0" Grid.Row="1"  Margin="30,11,138,0" x:Name="winHost" Height="58" VerticalAlignment="Top" OpacityMask="Transparent">            
        <win:DataGridView x:Name="dataGridView"></win:DataGridView>
    </my:WindowsFormsHost>        
    <GridSplitter  BorderThickness="1" Panel.ZIndex="1" Grid.Row="0" HorizontalAlignment="Stretch" Height="5" ShowsPreview="True" VerticalAlignment="Bottom">
    </GridSplitter>
</Grid>

现在只需调整边距以确保按钮和网格拆分器之间有一些空间。

于 2009-04-06T18:59:50.780 回答
0

解决方案是在网格拆分器中添加一个“Windows 窗体”标签,并在添加 DataGridView 后以编程方式执行此操作,使其显示在其顶部,如下所示:

void AddLabelToSplitter()
{
string template =
    @" <ControlTemplate 
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:mn='clr-namespace:MyNameSpace;assembly=MyAssembly'  
xmlns:wf='clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms'   
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' TargetType='{x:Type GridSplitter}'>
            <mn:ExtendedWindowsFormsHost x:Name='Grid_Splitter_WindowsFormsHost' HorizontalAlignment='Stretch'  VerticalAlignment='Stretch'>
                <wf:Label Dock='Fill' BackColor='DarkGray'></wf:Label>
            </mn:ExtendedWindowsFormsHost>
        </ControlTemplate>";
Grid_Splitter.Template = (ControlTemplate)XamlReader.Parse(template);
}

使用常规的 windows 窗体主机将不起作用,因为它不会将鼠标事件传递给拆分器,因此请使用以下链接中的 ExtendedWindowsFormsHost 代替:

保持鼠标事件从 WindowsFormsHost 冒泡

于 2015-06-01T08:25:26.450 回答