WindowsFormsHosts is not displayed on WPF page

61 views Asked by At

I am using WindowsFormsHosts to make a DataGridView. If I do the same thing on a separate window, everything displays and works fine, but if I do it on a page, WindowsFormsHosts does not display. Initially I have a Frame on MainWindow for selecting employee's department, after selecting department a page with WindowsFormsHosts and other WPF elements is loaded in another Frame.

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>

I've tried doing everything similar with a window rather than a page and everything displayed fine.

1

There are 1 answers

0
reseeman On

The problem was because I was using the StyledWindow style from the corresponding package on this window (MainWindow.xaml). I removed the StyledWindow style and everything was displayed correctly. Be careful with styles even when it seems like they shouldn't affect anything...