在WPF中使用WINFORM控件

合集下载

winform实现MVVM

winform实现MVVM

winform实现MVVM 1. 实现一个VM模型public class Person : INotifyPropertyChanged {#region 字段private string name;private int age;private int phone;#endregion#region 属性public string Name{get{return name;}set{name = value;OnPropertyChanged("Name");}}public int Age{get{return age;}set{age = value;OnPropertyChanged("Age");}}public int Phone{get{return phone;}set{phone = value;OnPropertyChanged("Phone");}}#endregionpublic event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string PropName) {if(PropertyChanged!=null){PropertyChanged.Invoke(this, new PropertyChangedEventArgs(PropName));}}}模型类必须引用INotifyPropertyChanged接口2. 窗口控件[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JYmdG2WD-)(…/Image/image-.png)]每个Textbox的Tag属性分别设置为对象的属性名3. 窗体代码public partial class FrmMain : Form{private Person VModel;public FrmMain(){InitializeComponent();VModel = new Person(){Age = 16,Name = "SHH",Phone = 123};}/// <summary>/// 递归获取所有控件/// </summary>/// <param name="ctl"></param>/// <returns></returns>private List<Control> GetAllControl(Control ctl){List<Control> result = new List<Control>(); if (ctl.HasChildren){if (!(ctl is Form)) result.Add(ctl);foreach (Control item in ctl.Controls) {result.AddRange(GetAllControl(item));}}else{result.Add(ctl);}return result;}/// <summary>/// 重写Load事件,绑定数据/// </summary>/// <param name="e"></param>protected override void OnLoad(EventArgs e){base.OnLoad(e);List<Control> list = GetAllControl(this);var propes =VModel.GetType().GetProperties();try{foreach (Control item in list){var prop = propes.FirstOrDefault(a => item.Tag != null && == item.Tag.ToString());if (prop != null){if (item is TextBox)item.DataBindings.Add(new Binding("Text", VModel, ));}}}catch (Exception){throw;}}private void btnShow_Click(object sender, EventArgs e){MessageBox.Show(Newtonsoft.Json.JsonConvert.SerializeO bject(VModel));}private void btnChange_Click(object sender, EventArgs e){VModel.Age = 100;VModel.Phone = 9999; = "laksjdlks";}}4. 实现思路1.VM模型引用INotifyPropertyChanged接口,当属性发生改变时,即执行了set访问器,触发事件2.将控件的tag属性与模型属性一一匹配3.遍历窗体控件,实现界面和模型数据的双向绑定5. 注意事项1.这里只是实现了双向绑定,单向影响没有实现2.ViewModel与Model之间的转换没有实现3.GetAllControl方法并不能获取所有控件4.不同类型的属性值需要绑定不同的控件,这个需要另外一一测试参考网站。

MFC与.NET及调用WinForm

MFC与.NET及调用WinForm

创建 .NET 用户控件
创建名为 WindowsControlLibrary1 的新 Visual C# Windows 控件库项目。 从“文件”菜单中,选择“新建”,然后选择“项目”。在“Visual C#”文件 夹中,选择“Windows 控件库”图标。 通过单击“确定”,接受默认项目名称 WindowsControlLibrary1。 .NET 控件的默认名称将为 UserControl1。 向 UserControl1 添加子控件。 在“工具箱”中,打开“所有 Windows 窗体”列表。将按钮控件拖动到 UserControl1 设计图面。 再添加一个 TextBox 控件。 在文件 UserControl1.Designer.cs 中,将 TextBox 和 Button 的声明从私有更 改为公共。 生成项目。 从“生成”菜单中,单击“生成解决方案”。 您可能希望在生成日志中记下生成的 DLL 的完整路径和文件名,因为您要将 这些信息输入 MFC 应用程序中。
文档
当从外壳程序打开一个文档时,MFC 将注册这些文档文件类型并处理此文档 类型。Windows 窗体则不支持文档。
文档状态
MFC 会维持文档的已更新状态。因此,当关闭应用程序、关闭包含应用程序 的最后一个视图或退出 Windows 时,MFC 将提示您保存文档。Windows 窗 体则不具有这样的支持。
创建 MFC 宿主应用程序
创建新 MFC 应用程序项目。
从“文件”菜单中,选择“新建”,然后选择“项目”。在“Visual C++”文件夹中,选 择“MFC 应用程序”图标。 在“名称”框中,输入 MFC01。将解决方案的设置更改为“添入解决方案”。单击“确 定”。将出现“MFC 应用程序向导”。 在“MFC 应用程序向导”中,选择“应用程序类型”。选择“基于对话框”。接受保留 默认设置并单击“完成”。这将创建带有 MFC 对话框的 MFC 应用程序。

WPF常用控件用法及介绍

WPF常用控件用法及介绍

WPF常⽤控件⽤法及介绍1.GroupBox注意: GroupBox仍然需要布局容器来放置元素。

如: StackPanel⾯板<GroupBox Header="select number?"><StackPanel><RadioButton>one</RadioButton><RadioButton>two</RadioButton><RadioButton>three</RadioButton></StackPanel></GroupBox>2.TabControl像这种标签页控件, 在winform种⾮常常见, Tabpge⼦页⾯, ⽽在WPF种, 对应的则是TabItem类。

所⽰的代码⽰意图:<TabControl><TabItem Header="⾸页"><StackPanel><Button>button1</Button><Button>button2</Button><Button>button3</Button></StackPanel></TabItem><TabItem Header="第⼆页"><StackPanel><Button>button4</Button><Button>button5</Button><Button>button6</Button></StackPanel></TabItem></TabControl>与Content属性相同, TabItem的Header同样可以接收任何类型的对象。

WinForm常用窗体控件的使用

WinForm常用窗体控件的使用
14
(8)BackColor: 用来获取或设置控件的背景色。 (9)Font:
用来获取或设置控件的字体,包括字体名称、字号以
及是否以斜体、加粗和下划线显示。 (10)Locked: 确定是否可以移动控件或者调整控件的大小。 (11)TabIndex: 用来获取或设置控件的Tab键顺序。
15
(12)TextAlign: 用来确定文本对齐方式。
4、为button1的click事件填写代码为
label1.visible=false; button1.enabled=false;
button2.enabled=true; 为button2的click事件填写代码为 label1.visible=true; button1.enabled=true; button2.enabled=false;
24
例:MouseEnter事件练习。 private void button1_MouseEnter(object sender, System.EventArgs e) { //颜色在System.Drawing名称空间下 button1.BackColor=Color.Red; }
25
例:MouseLeave事件练习。
Visual C# 程序设计
常见窗体控件的使用
1
本章中我们将主要介绍以下内容: windows窗体 标签 按钮 文本框 单选按钮 复选按钮 列表框 组合框 ToolTip控件 PictureBox控件
2
利用C#编写GUI(Graphics User Interface)应用 程序的一般步骤: (1)建立“Windows应用程序”项目 (2)设计应用程序界面
11
5. 2 WinForms中的常用控件

如何在WPF中使用WINDOWS FORM控件

如何在WPF中使用WINDOWS FORM控件

如何在WPF中使用Windows Form控件由于WPF中没有没有DateTimePicker控件,网上有人写的又不太好用,而在WinForm中有此控件,因此想到了从.net中借用此控件。

首先,我们需要向项目中的引用(reference)中添加两个动态库dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration。

组件不好找,排一下序就比较容易了。

添加完两个动态dll以后,就可以在控件库中找到WindowsFormsHost这个控件了。

这个控件是我们添加Windows Form控件的基础。

跟别的其他的控件一样,它也是可控的,可以自定义它在窗口中的位置、控件大小颜色等属性。

将这个控件放入窗体,放置完以后在xmal代码中会自动生成相应代码:<Grid><WindowsFormsHost Height="100"HorizontalAlignment="Left"Margin="36,29,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="200" /></Grid>然后,需要在xmal的开始处添加两行代码xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=W indowsFormsIntegration"xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Wi ndows.Forms"这样就可以在WindowsFormsHost下放置需要的Windows Form控件了,比如<WindowsFormsHost Height="196"HorizontalAlignment="Left"Margin="104,65,0,0" Name="windowsFormsHost1"VerticalAlignment="Top"Width="286"><WinFormControls:Button Text="WinformButton"Width="150"/></WindowsFormsHost>这是最简单的情况,就是添加了一个button。

wpf调用用户控件方法

wpf调用用户控件方法

wpf调用用户控件方法在WPF中,调用用户控件的方法通常涉及到对该控件的实例化以及对该实例的直接访问。

以下是一个简单的步骤说明如何调用用户控件的方法:1. 首先,确保你的用户控件已经正确地在你的项目中注册。

这通常通过在XAML中定义控件并赋予一个名称来完成。

例如:```xml<local:MyUserControl x:Name="myUserControl"/>```这里的`local`是用户控件所在命名空间的别名,`MyUserControl`是用户控件的类名,`x:Name`赋予了控件一个实例名`myUserControl`。

2. 在你的代码后面文件中(例如MainWindow.xaml.cs),你可以通过控件名来访问这个实例并调用其方法。

例如:```csharpmyUserControl.MyMethod();```这里假设`MyMethod`是你想要调用的用户控件的方法。

请注意,你的用户控件需要是public的,并且你想要调用的方法也必须是public的,这样你才能从其他类中访问到它们。

如果方法是private或protected的,那么你只能在定义它们的类或派生类中访问这些方法。

另外,如果用户控件是在某个容器(如Grid、StackPanel等)中的,你可能需要先获取对容器的引用,然后通过容器来访问该控件。

例如,如果控件是在名为`gridContainer`的Grid中,你可以这样获取:```csharpvar myUserControl = gridContainer.FindName("myUserControl") as MyUserControl;if(myUserControl != null){myUserControl.MyMethod();}```在这里,`FindName`方法用于查找指定名称的元素,然后我们将其转换为`MyUserControl`类型。

winform tablelayoutpanel用法

winform tablelayoutpanel用法

winform tablelayoutpanel用法一、WinForm简介WinForm(Windows Forms)是微软提供的一种桌面应用程序开发技术,基于Visual Studio开发环境。

它允许开发者创建具有丰富图形用户界面的应用程序,方便用户进行操作和交互。

在WinForm中,有许多控件可供开发者使用,从而实现各种功能。

本文将重点介绍TableLayoutPanel控件的用法。

二、TableLayoutPanel概述TableLayoutPanel是一款非常实用的控件,它类似于表格布局,可以轻松地组织和管理控件。

TableLayoutPanel具有如下特点:1.结构灵活:TableLayoutPanel可以自适应调整行列数,以满足不同布局需求。

2.控件间距可调:通过设置间距属性,可以调整控件之间的距离,使布局更美观。

3.支持合并单元格:可以合并相邻的单元格,以实现更复杂的布局效果。

4.易于调整:通过拖拽单元格或调整属性,可以轻松地修改布局。

三、TableLayoutPanel常用属性与方法1.属性:(1)Columns:设置TableLayoutPanel的列数。

(2)Rows:设置TableLayoutPanel的行数。

(3)CellBorderStyle:设置单元格边框样式。

(4)CellSpacing:设置单元格间距。

(5)BackColor:设置TableLayoutPanel背景颜色。

2.方法:(1)Add:向TableLayoutPanel中添加控件。

(2)AddRange:向TableLayoutPanel中批量添加控件。

(3)Remove:从TableLayoutPanel中移除控件。

(4)Clear:清空TableLayoutPanel中的所有控件。

四、TableLayoutPanel实例应用以下是一个简单的TableLayoutPanel实例应用,用于创建一个具有按钮、标签和文本框的登录界面:1.新建一个WinForm项目。

在WPF中自定义控件

在WPF中自定义控件
成一个 UserControl 并修改其外观,然后后台逻辑代码直接访问这些子元素.其最大的弊端在于:其对模板样式等支持度不好,其重复使用的范围有限. CustomControl, 其开发出来的控件才真正具有 WPF 风格,其对模板样式有着很好的支持,这是因为打造 CustomControl 时做到了逻辑代码与外观相分离,
承,打造 CustomControl 时其会为从 System.Windows.Controls.Control 继承.但实际情况下,也许我们从他们的衍生类别开始继承会得到更多的好处(更好的重 用已 有的逻辑),比如你的控件拥有更多的类似于 Button 的某些特性,那么从 Button 开始继承就比从 Control 继承少写很多代码.
二,UserControl 还是 CustomControl? 要在 WPF 中自定义一个控件,使用 UserControl 与 CustomControl 都是 不错的选择(除此之外,还有更多选择,比如打造一个自定义的面板,但这不在本文
的讨论范围),他们的区别在于: UserControl,其更像 WinForm 中自定义控件的开发风格,在开发上更简单快速,几乎可以简单地理解为:利用设计器来将多个已有控件作为子元素来拼凑
在 VS 中右键单击你的项目,点击"添加新项目",在出现的选择列表中选择"UserControl",VS 会自动为你生成一个*.xaml 文件以及其对应 的后台代码文件
2
(*.cs 或其它). 值得注意的是,自动生成的代码中,你的控件是继承于 erControl 类的,这对应你的控件而 言并不一定是最恰当的基 类,你可以修改它,但注意你应该同时修改*.cs 文件和*.xaml 文件中的基类,而不只是修改*.cs 文件,否则当生成项目时会 报错"不是继承于同一基类".修改 *.xaml 文件的方法是:将该文件的第一行和最后一行的"UserControl" 改成与你认为恰当的基类名称.
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
<wf:Label x:Name="lblName" Text="WinForm控件在此” /> </wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
<wf:Button x:Name="nnn" Text="确定” /> </wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
<wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right"> <wf:Label x:Name="lblName" Text="winForm控件在此” />
</wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
using System.Collections.Generic; usim.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;
public MainWindow() {
InitializeComponent(); }
private void button1_Click(object sender, RoutedEventArgs e) {
lblName.Text =”我是WinForm控件”; }
private void Window_Loaded(object sender, RoutedEventArgs e) {
// Generate a red curve with diamond // symbols, and "Porsche" in the legend LineItem myCurve = myPane.AddCurve("Porsche",
list1, System.Drawing.Color.Red, SymbolType.Diamond);
PointPairList list1 = new PointPairList(); PointPairList list2 = new PointPairList();
for (int i = 0; i < 36; i++) {
double x = (double)i + 5; double y1 = 1.5 + Math.Sin((double)i * 0.2); double y2 = 3.0 * (1.5 + Math.Sin((double)i * 0.2)); list1.Add(x, y1); list2.Add(x, y2); }
<zedgraph:ZedGraphControl x:Name="zedGraphControl" /> </wfi:WindowsFormsHost> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75"
zedGraphControl.AxisChange(); } } } 完整XAML如下: <Window x:Class="ZedGraphAtWpf2.MainWindow" xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph" xmlns="/winfx/2006/xaml/presentation" xmlns:x="/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Grid> <wfi:WindowsFormsHost Width="300" HorizontalAlignment="Right">
// Generate a blue curve with circle // symbols, and "Piper" in the legend LineItem myCurve2 = myPane.AddCurve("Piper",
list2, System.Drawing.Color.Blue, SymbolType.Circle);
ZedGraph.GraphPane myPane = zedGraphControl.GraphPane;
myPane.Title.Text = "My Test Graph\n(For CodeProject Sample)"; myPane.XAxis.Title.Text = "My X Axis"; myPane.YAxis.Title.Text = "My Y Axis";
在 WPF 中使用 WinForm 控件方法: 下面以在 Wpf 中添加 ZedGraph(用于创建任意数据的二维线型、条型、饼型图表的一个开 源类库)控件,说明在 WPF 中使用 Winform 控件的方法。 1、 首 先 添 加 对 如 下 两 个 dll 文 件 的 引 用 : WindowsFormsIntegration.dll ,
System.Windows.Forms.dll。 2、 由于要用到 ZedGraph 控件,所以也要添加对 ZedGraph.dll 的引用。 3、 在要使用 WinForm 控件的 WPF 窗体的 XAML 文件中添加如下内容(选中部分):
即: xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:zedgraph="clr-namespace:ZedGraph;assembly=ZedGraph" 4、 在WPF的容器控件内如Grid内首先要添加WinForm控件的宿主容器,用于衔接WPF和WinForm, 对应XAML如下: <Grid>
using System.Windows.Forms; using ZedGraph; using System.Drawing;
namespace ZedGraphAtWpf2 {
/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window {
Click="button1_Click" /> </Grid>
</Window>
<wf:Button x:Name="nnn" Text="确定”/> </wfi:WindowsFormsHost> <wfi:WindowsFormsHost>
<zedgraph:ZedGraphControl x:Name="zedGraphControl" /> </wfi:WindowsFormsHost> <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> 说明:<wfi:WindowsFormsHost></wfi:WindowsFormsHost>即为WinForm控件的宿主容器,每一个宿主 容器只能放一个WinForm控件,如下例,放了三个WinForm控件,分别放在三个宿主容器里面,该容器 可以设置属性来调整大小和布局。 注意:如上我添加的WinForm控件如在指定其Name时,必须加前缀x:,如添加Lable时 <wf:Label x:Name="lblName" Text="我是WPF中的WinForm控件” />,否则后台代码无法访问。 5、 如果要在WPF后台代码中访问上面的Lable,可直接像在WinForm中使用一样。如在点击某一按钮 时改变Lable内容,代码如下:lblName.Text=”内容已改变”; 6、 以使用WinForm控件ZedGraph绘制曲线为例,说明后台用法: 在后台用Using添加对System.Windows.Forms和ZedGraph和System.Drawing命名空间的引用,完 整后台代码如下: using System;
相关文档
最新文档