Results 1 to 1 of 1

Thread: WinForms controls cannot be displayed in WPF windows when AllowsTransparency is true

  1. #1

    Thread Starter
    Addicted Member Kram Kramer's Avatar
    Join Date
    Dec 2016
    Posts
    131

    WinForms controls cannot be displayed in WPF windows when AllowsTransparency is true

    1- Create Wpf/C# project and named it as WpfApplication1.

    2- Add the following references to the project.

    System.Windows.Forms
    WindowsFormsIntegration


    3- Copy and paste the following codes into MainWindow.xaml file.

    Code:
    <Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525" ContentRendered="Window_ContentRendered">
    <Grid>
        <WindowsFormsHost>
            <wf:DataGridView x:Name="DataGridView1">                
                <wf:DataGridView.Columns>
                    <wf:DataGridViewTextBoxColumn HeaderText="Name"/>
                    <wf:DataGridViewTextBoxColumn HeaderText="Sur Name"/>
                </wf:DataGridView.Columns>
            </wf:DataGridView>
        </WindowsFormsHost>
    </Grid>
    </Window>
    4- Copy and paste the following codes into MainWindow.xaml.cs file.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication1
    {
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            DataGridView1.Rows.Add("Albert", "Einstein");
            DataGridView1.Rows.Add("Nikola", "Tesla");
            DataGridView1.Rows.Add("Isaac", "Newton");
        }
    }
    }
    5- When you run this project you are able to see DataGridView1 correctly.

    6- Add following codes into Window tag.

    Code:
    WindowStyle="None" AllowsTransparency="True"
    7- When you run this project again you are not able to see DataGridView1 correctly!!!

    The following link says:

    WinForms controls cannot be displayed in WPF windows that have the AllowsTransparency property set to true.

    https://stackoverflow.com/questions/...t-are-the-risk

    The following link says there is a solution

    https://stackoverflow.com/questions/...y-true/4660861

    https://blogs.msdn.microsoft.com/cha...nt-wpf-window/

    But I am not able to understand what the solution is...

    P.S. I am using .Net Framework 4.7.2
    Last edited by Kram Kramer; Oct 12th, 2019 at 10:08 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width