将标签绑定到变量 [英] Binding label to a variable

查看:88
本文介绍了将标签绑定到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从WPF开始,我正在尝试在局部变量和标签之间设置绑定.基本上,我想在局部变量更改时更新标签.我一直在寻找解决方案,但他们都只是将文本框用作源,而不仅仅是类变量,我甚至不确定它是否可以这种方式工作.这是我的代码.

I am just starting with WPF and I am trying to setup binding between a local variable and a label. Basicaly I want to update the label when local variable changes. I was searching for solution but they all just use textbox as a source not just class variable and I am not even sure it works this way. So here is my code.

public partial class MainWindow : Window
{
    int idCounter;

    public MainWindow()
    {
        InitializeComponent();


        Binding b = new Binding();
        b.Source = idCounter;
        b.Mode = BindingMode.OneWay;
        b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

        myLabel.SetBinding(Label.ContentProperty,b);
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        idCounter++;
    }
}

按钮确实起作用,idCounter更改了值,但是它在标签中没有更新,因此我认为绑定是错误的.有人可以告诉我怎么了吗?谢谢

Button does work, idCounter changes value, but it does not update in label so I guess binding is wrong. Can someone tell me what is wrong? Thanks

推荐答案

如果您将类更改为此代码,则您的代码将起作用.

Your code will work if you change your class to this...

  public partial class Window1 : Window, INotifyPropertyChanged
    {
        private int _idCounter;
        public int IdCounter
        {
            get { return _idCounter; }
            set
            {
                if (value != _idCounter)
                {
                    _idCounter = value;
                    OnPropertyChanged("IdCounter");
                }
            }
        }
        public Window1()
        {
            InitializeComponent();
            myLabel.SetBinding(ContentProperty, new Binding("IdCounter"));
            DataContext = this;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            IdCounter++;
        }
        #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string name)
        {
            var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }

您遇到的一些问题...

Some of the issues you were having are...

窗口本身应实现INotifyPropertyChanged,以便绑定引擎可以将其放在耳朵上.

The window itself should implement INotifyPropertyChanged so that the binding engine can place an ear on it.

IdCounter必须是公共的并且具有公共获取器,以便绑定引擎可以获取"它.

the IdCounter needs to be public and have a public getter on it so that the binding engine can 'get' it.

您应该将DataContext设置为声明IdCounter的任何类(在本例中为MainWindow).部分原因是绑定引擎没有DataContext.

You should set the DataContext to whatever class has declared IdCounter (the MainWindow in this case). Part of the problem was that the binding engine had no DataContext.

由于默认情况下,Label会以这种方式进行绑定,因此BindingMode设置是一条红色提示.

The BindingMode setting was a red-herring since a Label binds that way by default.

UpdateSourceTrigger是一条红色鲱鱼,因为标签的内容没有更新源属性的机制.标签的内容不像文本框,用户可以在其中键入代码需要了解的内容.当您绑定到用户无法更改的内容时,请别忘了UpdateSourceTrigger,最重要的是 Target 属性.

The UpdateSourceTrigger was a red-herring since the content of the label does not have a mechanism to update the source property. A label's content is not like a text box where the user can type something that the code needs to know about. When you're binding to something that the user cannot change, forget about UpdateSourceTrigger, it's the Target property that counts.

处理程序应标记该事件.这是一个好习惯,并且不会影响绑定.

The handler should mark the event. This is good practice and did not affect the binding.

绑定构造函数仅需要路径.

The binding constructor needs only the path.

此代码将为您提供预期的结果;即,单击按钮时标签会更新.在.net 4.5的vs2013上检查,编译和执行.

This code will give you your expected result; i.e., that the label updates when the button is clicked. Checked, compiled, and executed on vs2013, .net 4.5.

其他受访者表示您应该使用视图模型.我同意这一100%的要求,总的来说,这是一件好事.

The other respondents said you should use a View Model. I agree with this 100%, and overall it's a good thing to consider.

这篇关于将标签绑定到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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