结合在winform像在WPF [英] Binding in winform like in WPF

查看:146
本文介绍了结合在winform像在WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个winform的窗体的宽度属性绑定使标签的文本被更新我尽了一切鼠标移动的标签上为文本。目前我只实现更新单击窗体上的一些元素时而不continious更新(比如,如果你改变调整大小处理文本)。 ?

I want to bind a winform's form's Width property to Text on a label so label's text gets updated every mouse movement I made. Currently I only achieved updating when some element on a form is clicked but not continious updating(like if you change text in Resize handler). How to do this thing?

推荐答案

您可以通过该做绑定到Width属性

You can bind to the Width property by doing this:

label1.DataBindings.Add(new Binding("Text", this, "Width"));



问题有形式不通知该属性已改变的框架。你最简单最好的选择很可能只是做了香饽饽方式:

The problem there is the form isn't notifying the framework that the property has changed. Your easiest best bet is likely to just do it the meat and potatoes way:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    label1.Text = this.Width.ToString();
}



编辑:好吧,如果你真的想使用数据绑定,这里是工作(但就像是达到你的头部周围划伤你的耳朵)的方式:

Okay, if you really want to use data binding, here is a way that works (but is like reaching around your head to scratch your ear):

一个对象的数据源添加到您的形式和设置。数据源输入System.Windows.Forms.Form中

Add an object data source to your form and set the DataSource to type "System.Windows.Forms.Form".

接下来,添加一些代码:

Next, add some code:

public Form2()
{
   InitializeComponent();

   this.formBindingSource.DataSource = this;

   Binding binding = new Binding("Text", this.formBindingSource, "Size", true);

   binding.Format += new ConvertEventHandler(binding_Format);

   label1.DataBindings.Add(binding);
}

void binding_Format(object sender, ConvertEventArgs e)
{
    Size size = (Size)e.Value;
    e.Value = size.Width.ToString();
}



所以,就像我说的,这是完整的矫枉过正,但它的工作原理。

So like I said, it's complete overkill, but it works.

这篇关于结合在winform像在WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆