wpf WPF绑定到局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1481130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:02:11  来源:igfitidea点击:

WPF Binding to local variable

wpfdata-bindingbinding

提问by PrimeTSS

Can you bind to a local variable like this?

你能绑定到这样的局部变量吗?

SystemDataBase.cs

系统数据库

namespace WebWalker
{
    public partial class SystemDataBase : Window
    {
        private string text = "testing";
...

SystemDataBase.xaml

系统数据库.xaml

 <TextBox 
       Name="stbSQLConnectionString" 
       Text="{SystemDataBase.text}">
 </TextBox>

??

??

Text is set to the local variable "text"

文本设置为局部变量“文本”

回答by

The pattern is:

图案是:

public string Text {get;set;}

and the binding is

绑定是

{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}

If you want the binding to update automatically you should make it a DependencyProperty.

如果您希望绑定自动更新,您应该将其设为 DependencyProperty。



I think 3.5 added ElementNameto bindings, so the following is a little easier:

我认为 3.5 添加ElementName到绑定中,因此以下内容更容易一些:

<Window x:Name="Derp" ...
  <TextBlock Text="{Binding Text, ElementName=Derp}"/>

回答by Danny Varod

To bind to a local "variable" the variable should be:

要绑定到本地“变量”,变量应该是:

  1. A property, not a field.
  2. Public.
  3. Either a notifying property (suitable for model classes) or a dependency property (sutable for view classes)
  1. 一个属性,而不是一个字段。
  2. 民众。
  3. 通知属性(适用于模型类)或依赖属性(适用于视图类)

Notifying property example:

通知属性示例:

public MyClass : INotifyPropertyChanged
{
    private void PropertyType myField;

    public PropertyType MyProperty
    {
        get
        {
            return this.myField;
        }
        set
        {
            if (value != this.myField)
            {
                this.myField = value;
                NotifyPropertyChanged("MyProperty");
            }
        }
    }

    protected void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Dependency property example:

依赖属性示例:

public MyClass : DependencyObject
{
    public PropertyType MyProperty
    {
        get
        {
            return (PropertyType)GetValue("MyProperty");
        }
        set
        {
            SetValue("MyProperty", value);
        }
    }

    // Look up DependencyProperty in MSDN for details
    public static DependencyProperty MyPropertyProperty = DependencyProperty.Register( ... );
}

回答by Neil

If you're doing a lot of this, you could consider binding the DataContext of the whole window to your class. This will be inherited by default, but can still be overridden as usual

如果你做了很多这样的事情,你可以考虑将整个窗口的 DataContext 绑定到你的类。这将默认继承,但仍然可以像往常一样被覆盖

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}">

Then for an individual components you can use

然后对于单个组件,您可以使用

Text="{Binding Text}"

回答by AnjumSKhan

To bind a local variable which is present in your Window class it has to be : 1. Public property 2. A notifying property. For this your window class should implement INotifyPropertyChanged interface for this property.

要绑定存在于 Window 类中的局部变量,它必须是: 1. 公共属性 2. 通知属性。为此,您的窗口类应为此属性实现 INotifyPropertyChanged 接口。

Then in the constructor

然后在构造函数中

public Assgn5()
{           
    InitializeComponent();

    this.DataContext = this; // or **stbSQLConnectionString**.DataContext = this;
}

 <TextBox 
   Name="stbSQLConnectionString" 
   Text="{Binding text}">
 </TextBox>