WindowsFormsHosts 未显示在 WPF 页面上

问题描述 投票:0回答:1

我正在使用 WindowsFormsHosts 来制作 DataGridView。如果我在单独的窗口上执行相同的操作,则一切都会显示并正常工作,但如果我在页面上执行此操作,则 WindowsFormsHosts 不会显示。最初,我在主窗口上有一个框架用于选择员工的部门,选择部门后,包含 WindowsFormsHosts 和其他 WPF 元素的页面将加载到另一个框架中。

MainWindow.cs

public MainWindow()
{
    InitializeComponent();
    Odb.db = new System.Data.Entity.DbContext("my connectionString");

    FrameApp.SetCurrentMainFrame(MainFrame);
    FrameApp.SetCurrentTopFrame(TopFrame);

    FrameApp.FrameTop.Navigate(new ChooseDepPage());
}

FrameApp.cs

internal class FrameApp
{
    public static Frame FrameMain { get; set; }
    public static Frame FrameTop { get; set; }

    public static void SetCurrentMainFrame(Frame frame) => FrameMain = frame;

    public static void SetCurrentTopFrame(Frame frame) => FrameTop = frame;

    public static void NavigateToPageMain(Page page)
    {
        if (FrameMain != null)
            FrameMain.Navigate(page);
            //FrameObj.Content = page;
        else
            throw new InvalidOperationException("The current Frame has not been set. Set it using SetCurrentFrame.");
    }

    public static void NavigateToPageTop(Page page)
    {
        if (FrameTop != null)
            FrameTop.Navigate(page);
            //FrameObjSec.Content = page;
        else
            throw new InvalidOperationException("The current Frame has not been set. Set it using SetCurrentFrame.");
    }
}

ChooseDepPage.cs

private void DepLV_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SelectedDep = (DepModel)DepLV.SelectedItem;
    if (SelectedDep != null)
    {
        SearchDepTBX.Text = $"{SelectedDep.Position}";
        MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
        mainWindow.MainFrame.Navigate(new SelectedDepPageVer2(SelectedDep));
    }
}

SelectedDepPageVer2.xaml

<Page x:Class="PlanningScheduleApp.Pages.SelectedDepPageVer2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:PlanningScheduleApp.Pages"
      xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
      xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
      xmlns:WindowsFormsHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
      mc:Ignorable="d" 
      d:DesignHeight="650" d:DesignWidth="1200"
      Title="SelectedDepPageVer2">
      <Grid x:Name="grid1">
          <Grid.RowDefinitions>
               <RowDefinition Height="0.10*"/>
               <RowDefinition Height="*"/>
               <RowDefinition Height="20"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
               <ColumnDefinition Width="*"/>
               <ColumnDefinition Width="0.24*"/>
          </Grid.ColumnDefinitions>
          <WindowsFormsHost Grid.Row="1" Grid.Column="0">
               <wf:DataGridView x:Name="StaffDGV" RowHeadersVisible="False" ReadOnly="True" SelectionMode="FullRowSelect" AllowUserToResizeRows="False" AutoSizeColumnsMode="Fill" DoubleClick="StaffDGV_DoubleClick" SelectionChanged="StaffDGV_SelectionChanged" AutoGenerateColumns="False"/>
          </WindowsFormsHost>
      </Grid>
</Page>

我尝试过用窗口而不是页面做类似的事情,并且一切都显示得很好。

c# .net wpf winforms hybrid
1个回答
0
投票

您的问题似乎归因于

WindowsFormsHost
DataGridView
但您是否检查过以确保 anything 可见(例如通过将顶级网格设置为可识别的背景颜色)?我没有看到
Content
被设置,我想知道它是否像纠正它一样简单?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var frame = new Frame();
        FrameApp.SetCurrentMainFrame(frame);
        FrameApp.SetCurrentTopFrame(frame);
        FrameApp.FrameTop.Navigate(new ChooseDepPage());

        Content = frame; // << Check that content is being set "somewhere"
    }
}

最少的尝试(但未能)重现问题


public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var frame = new Frame();
        FrameApp.SetCurrentMainFrame(frame);
        FrameApp.SetCurrentTopFrame(frame);
        Content = frame; // << Check that content is being set "somewhere"
        FrameApp.FrameTop.Navigate(new ChooseDepPage());
    }
}

class FrameApp
{
    public static Frame FrameMain { get; set; }
    public static Frame FrameTop { get; set; }
    public static void SetCurrentMainFrame(Frame frame) => FrameMain = frame;
    public static void SetCurrentTopFrame(Frame frame) => FrameTop = frame;
    public static void NavigateToPageMain(Page page)
    {
        if (FrameMain != null)
            FrameMain.Navigate(page);
        else
            throw new InvalidOperationException("The current Frame has not been set. Set it using SetCurrentFrame.");
    }
    public static void NavigateToPageTop(Page page)
    {
        if (FrameTop != null)
            FrameTop.Navigate(page);
        else
            throw new InvalidOperationException("The current Frame has not been set. Set it using SetCurrentFrame.");
    }
}

<Page x:Class="show_data_grid_view.ChooseDepPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:show_data_grid_view"
      xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
      mc:Ignorable="d" 
      d:DesignHeight="450" d:DesignWidth="800"
      Title="SelectedDepPageVer2">
        <Grid 
            x:Name="grid1"
            Background="LightGreen">
            <Grid.RowDefinitions>
                <RowDefinition Height="0.10*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="0.24*"/>
            </Grid.ColumnDefinitions>
            <WindowsFormsHost
                Grid.Row="1" 
                Grid.Column="0"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"
                Margin="10,10,10,10">
                <wf:DataGridView 
                    x:Name="StaffDGV" 
                    RowHeadersVisible="False"
                    ReadOnly="True" 
                    SelectionMode="FullRowSelect" 
                    AllowUserToResizeRows="False"
                    AutoSizeColumnsMode="Fill"
                    DoubleClick="StaffDGV_DoubleClick" 
                    SelectionChanged="StaffDGV_SelectionChanged"
                    AutoGenerateColumns="False"
                    BackgroundColor="WhiteSmoke"
                    Dock="Fill"/>
            </WindowsFormsHost>
        </Grid>
</Page>

public partial class ChooseDepPage : Page
{
    public ChooseDepPage()
    {
        base.DataContext = new ChooseDepPageModel();
        InitializeComponent();

        Loaded += async (sender, e) =>
        {
            // Turn ON for minimal test
            StaffDGV.AutoGenerateColumns = true;
            StaffDGV.DataSource = DataContext.Recordset;
            StaffDGV.Font = new Font("Segoe UI", 16 );
            StaffDGV.ColumnHeadersHeight = 60;
            StaffDGV.RowTemplate.Height = 60;
            StaffDGV.AllowUserToAddRows = false;
            DataContext.Recordset.Add(new Record { Index = 1, Description = "Record A" });
            DataContext.Recordset.Add(new Record { Index = 2, Description = "Record B" });
            DataContext.Recordset.Add(new Record { Index = 3, Description = "Record C" });
            await Task.Delay(100);
            StaffDGV.CurrentCell = null;
        };
    }
    ChooseDepPageModel DataContext => (ChooseDepPageModel)base.DataContext;
    private void StaffDGV_DoubleClick(object sender, EventArgs e) { }
    private void StaffDGV_SelectionChanged(object sender, EventArgs e) { }
}

class ChooseDepPageModel
{
    public BindingList<Record> Recordset { get; } = new BindingList<Record>();
}

class Record
{
    public int Index { get; set; }
    public string Description { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.