Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WinForms中具有浮动精度大小的WPF自定义控件_C#_Wpf_Wpf Controls - Fatal编程技术网

C# WinForms中具有浮动精度大小的WPF自定义控件

C# WinForms中具有浮动精度大小的WPF自定义控件,c#,wpf,wpf-controls,C#,Wpf,Wpf Controls,我想知道是否有可能在wpf中创建一个类似于面板的控件,以及另一个wpf自定义控件,它将是这个面板的子控件,所有这些都在WinForms应用程序中。然后,是否可以在float中设置自定义控件的大小? 我还能旋转那个自定义控件吗 提前感谢您将需要使用名称空间,更具体地说,将允许您在Winforms中嵌入WPF控件 这是更改WPF UserControls子UserControls大小和位置的快速而简单的演示。我将把动画留给你 主要Winform表单 using System; using Syst

我想知道是否有可能在wpf中创建一个类似于面板的控件,以及另一个wpf自定义控件,它将是这个面板的子控件,所有这些都在WinForms应用程序中。然后,是否可以在float中设置自定义控件的大小? 我还能旋转那个自定义控件吗

提前感谢

您将需要使用名称空间,更具体地说,将允许您在Winforms中嵌入WPF控件


这是更改WPF UserControls子UserControls大小和位置的快速而简单的演示。我将把动画留给你

主要Winform表单

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ElementHost host;
        WpfControlLibrary1.UserControl1 uc;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            host = new ElementHost();
            host.Dock = DockStyle.Top;
            uc = new WpfControlLibrary1.UserControl1();
            host.Child = uc;
            this.Controls.Add(host);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            uc.SetChildLocation =  new  System.Windows.Point(uc.SetChildLocation.X + 25.5, uc.SetChildLocation.Y + 10.2);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            uc.SetChildSize = new System.Windows.Size(uc.SetChildSize.Width + .25, uc.SetChildSize.Height + .25);
        }
    }
}
UserControl1.xaml

<UserControl x:Class="WpfControlLibrary1.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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Background="Red" xmlns:my="clr-namespace:WpfControlLibrary1">
    <Canvas>
        <my:UserControl2 HorizontalAlignment="Center"   x:Name="userControl21" VerticalAlignment="Center" Canvas.Left="0" Canvas.Top="0" />

    </Canvas>
</UserControl>
<UserControl x:Class="WpfControlLibrary1.UserControl2"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" SizeChanged="UserControl_SizeChanged">
    <Grid>
        <Rectangle x:Name="rect" Fill="Blue" Height="40" Width="120"></Rectangle>
    </Grid>
</UserControl>

UserControl1.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1  
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public Point  SetChildLocation
        {
            get
            {
                return new Point(Canvas.GetLeft(userControl21), Canvas.GetTop(userControl21));
            }

            set
            {
                Canvas.SetLeft(userControl21, value.X);
                Canvas.SetTop(userControl21, value.Y);
            }
        }

        public Size SetChildSize
        {
            get
            {
                return new Size(userControl21.ActualWidth, userControl21.ActualHeight);
            }
            set
            {
                userControl21.Width = value.Width;
                userControl21.Height = value.Height;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            rect.Width = e.NewSize.Width;
            rect.Height = e.NewSize.Height;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfControlLibrary1
{
/// 
///UserControl1.xaml的交互逻辑
/// 
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
}
公共点定位
{
得到
{
返回新点(Canvas.GetLeft(userControl21)、Canvas.GetTop(userControl21));
}
设置
{
SetLeft(userControl21,value.X);
SetTop(userControl21,value.Y);
}
}
公共大小设置ChildSize
{
得到
{
返回新的大小(userControl21.ActualWidth,userControl21.ActualHeight);
}
设置
{
userControl21.Width=value.Width;
userControl21.Height=value.Height;
}
}
}
}
UserControl2.xaml

<UserControl x:Class="WpfControlLibrary1.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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" Background="Red" xmlns:my="clr-namespace:WpfControlLibrary1">
    <Canvas>
        <my:UserControl2 HorizontalAlignment="Center"   x:Name="userControl21" VerticalAlignment="Center" Canvas.Left="0" Canvas.Top="0" />

    </Canvas>
</UserControl>
<UserControl x:Class="WpfControlLibrary1.UserControl2"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300" SizeChanged="UserControl_SizeChanged">
    <Grid>
        <Rectangle x:Name="rect" Fill="Blue" Height="40" Width="120"></Rectangle>
    </Grid>
</UserControl>

UserControl2.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1  
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public Point  SetChildLocation
        {
            get
            {
                return new Point(Canvas.GetLeft(userControl21), Canvas.GetTop(userControl21));
            }

            set
            {
                Canvas.SetLeft(userControl21, value.X);
                Canvas.SetTop(userControl21, value.Y);
            }
        }

        public Size SetChildSize
        {
            get
            {
                return new Size(userControl21.ActualWidth, userControl21.ActualHeight);
            }
            set
            {
                userControl21.Width = value.Width;
                userControl21.Height = value.Height;
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 WpfControlLibrary1
{
    /// <summary>
    /// Interaction logic for UserControl2.xaml
    /// </summary>
    public partial class UserControl2 : UserControl
    {
        public UserControl2()
        {
            InitializeComponent();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            rect.Width = e.NewSize.Width;
            rect.Height = e.NewSize.Height;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfControlLibrary1
{
/// 
///UserControl2.xaml的交互逻辑
/// 
公共部分类UserControl2:UserControl
{
公共用户控制2()
{
初始化组件();
}
私有void UserControl_SizeChanged(对象发送方,sizechangedventargs e)
{
rect.Width=e.NewSize.Width;
rect.Height=e.NewSize.Height;
}
}
}

感谢您的快速响应。我想请你帮个小忙,你能给我举个例子吗?我会非常感激。@user1806687我会看看我能想出什么谢谢,你是最棒的!:哇,非常感谢!当我尝试时,我会报复你的。再次感谢!:D@user1806687我创建了一个单独的WpfUserControl库项目,并向Winform项目添加了一个项目引用。