WPF数据绑定到字符串属性 [英] WPF Data Binding to a string property

查看:314
本文介绍了WPF数据绑定到字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于数据绑定的问题,我正在为此而苦苦挣扎.

I have a question about data binding which I am struggling with.

我的xaml.cs文件中具有以下属性:

I have the following property in my xaml.cs file:

    private string _stationIdInstruction;

    public event PropertyChangedEventHandler PropertyChanged;

    public string StationIdInstruction
    {
        get { return _stationIdInstruction; }
        set
        {
            _stationIdInstruction = value;
            OnPropertyChanged("StationIdInstruction");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }

如何将TextBlock绑定到StationIdInstructions,以便它在我更新StationIdInstructions时将字符串属性作为其Text并更新TextBlock.Text.

How can I bind a TextBlock to StationIdInstructions so it picks up the string property as its Text and update the TextBlock.Text when I update StationIdInstructions.

感谢您的帮助.

推荐答案

是的,不要忘记指定绑定上下文.例如,

Yes, and don't forget to specify the binding context. E.g.,

<Window ... Name="MyWindow">
  <Grid DataContext="{Binding ElementName=MyWindow, Path=.}">
    <TextBlock Text="{Binding Path=StationIdInstruction}" />

这篇关于WPF数据绑定到字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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