title: How can I include a wpf project to a windows form project in C#
body: <p>I used element host to host the wpf element which in my case is a toggle switch which I designed in a wpf project. Now I want to do something to the window if I click on the toggle switch. For example if the switch toggle to on display something like "Connected" in a textbox and if it's toggled to off display "Disconnected".</p>
  
  <p>I am absolutely new in C#, how can I achieve my goal?</p>
  
  <p>The described window:</p>
  
  <p><a href="https://i.stack.imgur.com/TEZgF.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TEZgF.jpg" alt="When disconnected"></a> <a href="https://i.stack.imgur.com/BkYa3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BkYa3.jpg" alt="When Connected"></a></p>
  
  <p>The xaml code:</p>
  
  <pre><code>&lt;UserControl x:Class="MyToggleBtn.UserControl1"
               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:MyToggleBtn"
               mc:Ignorable="d" Height="303.428" Width="838.571"&gt;
  
  
      &lt;Viewbox Margin="0"&gt;
          &lt;Grid x:Name="Back" HorizontalAlignment="Center" Height="280" Margin="0" VerticalAlignment="Center" Width="780"&gt;
              &lt;Rectangle x:Name="BtnBackGround" HorizontalAlignment="Left" Height="260" Margin="0,10,0,0" RadiusY="40.929" RadiusX="40.929" Stroke="Black" VerticalAlignment="Top" Width="770" Fill="#FFEE0D0D" MouseLeftButtonDown="BtnBackGround_MouseLeftButtonDown"/&gt;
              &lt;Label Content="OFF" HorizontalAlignment="Left" Margin="513,94,0,0" VerticalAlignment="Top" Height="98" Width="144" FontWeight="Bold" FontSize="72" FontFamily="Sitka Banner" Foreground="White"/&gt;
              &lt;Label Content="ON" HorizontalAlignment="Left" Margin="112,94,0,0" VerticalAlignment="Top" Height="98" Width="144" FontWeight="Bold" FontSize="72" FontFamily="Sitka Banner" Foreground="White"/&gt;
              &lt;Rectangle x:Name="Slider" HorizontalAlignment="Center" Height="260" RadiusY="40.929" RadiusX="40.929" Stroke="Black" VerticalAlignment="Center" Width="424" Margin="{DynamicResource Thickness2}" MouseLeftButtonDown="Slider_MouseLeftButtonDown"&gt;
                  &lt;Rectangle.Fill&gt;
                      &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt;
                          &lt;GradientStop Color="#FFA89696" Offset="0.857"/&gt;
                          &lt;GradientStop Color="#FFE8D8D8" Offset="0.6"/&gt;
                      &lt;/LinearGradientBrush&gt;
                  &lt;/Rectangle.Fill&gt;
              &lt;/Rectangle&gt;
          &lt;/Grid&gt;
      &lt;/Viewbox&gt;
  &lt;/UserControl&gt;
  
  
  xml.cs 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 MyToggleBtn
  {
      /// &lt;summary&gt;
      /// Interaction logic for UserControl1.xaml
      /// &lt;/summary&gt;
      public partial class UserControl1 : UserControl
      {
          Thickness LeftSide = new Thickness(-354, 0, 0, 0);
          Thickness RightSide = new Thickness(0, 0, -335, 0);
  
          SolidColorBrush Off = new SolidColorBrush(Color.FromRgb(238, 13, 13));
          SolidColorBrush On = new SolidColorBrush(Color.FromRgb(37, 174, 62));
          private bool Toggled = false;
  
          private void SwitchOn() {
              BtnBackGround.Fill = On;
              Toggled = true;
              Slider.Margin = RightSide;
          }
  
          private void SwitchOff() {
              BtnBackGround.Fill = Off;
              Toggled = false;
              Slider.Margin = LeftSide;
          }
  
          public UserControl1() {
              InitializeComponent();
              SwitchOff();
          }
  
  
  
          public bool Toggled1 { get =&gt; Toggled; set =&gt; Toggled = value; }
  private void Slider_MouseLeftButtonDown(object sender, RoutedEventArgs e)
          {
              if (!Toggled)
              {
                  SwitchOn();
              }
              else
              {
                  SwitchOff();
              }
          }
  
          private void BtnBackGround_MouseLeftButtonDown(object sender, RoutedEventArgs e)
          {
              if (!Toggled)
              {
                  SwitchOn();
              }
              else
              {
                  SwitchOff();
              }
          }
          //public bool getSlider { get =&gt; Slider_MouseLeftButtonDown(); }
      }
  }
  
  Form.cs code
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Windows.Forms.Integration;
  using System.Windows.Media;
  
  
  namespace VPN
  {
      public partial class MainForm : Form
      {
          public MainForm()
          {
              InitializeComponent();
          }
  
          public void ResetToDefault() {
              ConnState.Text = "Disconnected from server.";
              //ConnState.Location = new Point(222, 90);
              ConnState.AutoSize = true;
              ConnState.Font = new Font("Calibri", 12);
              ConnState.BorderStyle = BorderStyle.Fixed3D;
              ConnState.ForeColor = System.Drawing.Color.Red;
              ConnState.Padding = new Padding(3);
              ConnState.TextAlign = ContentAlignment.MiddleCenter;
              comboBox1.Text = "Please select sever";
          }
  
  
  
  
  
          private void MainForm_Load(object sender, EventArgs e)
          {
              // Create the ElementHost control for hosting the
              // WPF UserControl.
                 ElementHost host = new ElementHost();
                 host.Dock = DockStyle.Fill;
  
              // Create the WPF UserControl.
               /*HostingWpfUserControlInWf.UserControl1 uc =
                  new HostingWpfUserControlInWf.UserControl1();*/
  
              MyToggleBtn.UserControl1 uc = new MyToggleBtn.UserControl1();
  
              // Assign the WPF UserControl to the ElementHost control's
              // Child property.
                host.Child = uc;
  
               // Add the ElementHost control to the form's
               // collection of child controls.
               this.Controls.Add(host);
  
              if (!uc.Toggled1)
              {
                  ResetToDefault();
              }
              else
              {
                  uc.Toggled1 = true;
                  ConnState.Text = comboBox1.Text;
              }
          }
      }
  }][1]
  </code></pre>