Как я могу включить проект wpf в windows проект формы в C# - PullRequest
0 голосов
/ 03 марта 2020

Я использовал элемент host для размещения элемента wpf, который в моем случае является тумблером, который я разработал в проекте wpf. Теперь я хочу сделать что-то с окном, если я нажму на тумблер. Например, если переключатель включен, в текстовом поле отображается что-то вроде «Подключено», и если он отключен, отображается «Отключен».

Я абсолютно новичок в C#, как я могу достичь своей цели?

Описанное окно:

When disconnected When Connected

Код xaml:

<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">


    <Viewbox Margin="0">
        <Grid x:Name="Back" HorizontalAlignment="Center" Height="280" Margin="0" VerticalAlignment="Center" Width="780">
            <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"/>
            <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"/>
            <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"/>
            <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">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFA89696" Offset="0.857"/>
                        <GradientStop Color="#FFE8D8D8" Offset="0.6"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Grid>
    </Viewbox>
</UserControl>


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
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    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 => Toggled; set => 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 => 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]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...