WPF绑定到局部变量

WPF绑定到局部变量,wpf,data-binding,binding,Wpf,Data Binding,Binding,你能像这样绑定到一个局部变量吗 SystemDataBase.cs namespace WebWalker { public partial class SystemDataBase : Window { private string text = "testing"; ... SystemDataBase.xaml <TextBox Name="stbSQLConnectionString" Text="{SystemD

你能像这样绑定到一个局部变量吗

SystemDataBase.cs

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

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

?

Text设置为局部变量“Text”

模式为:

public string Text {get;set;}
而且绑定是

{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}
如果希望绑定自动更新,则应将其设置为DependencyProperty


我认为3.5在绑定中添加了
ElementName
,因此以下内容更简单一些:

<Window x:Name="Derp" ...
  <TextBlock Text="{Binding Text, ElementName=Derp}"/>
要绑定到本地“变量”,变量应为:

  • 属性,而不是字段
  • 公众
  • 通知属性(适用于模型类)或依赖属性(可用于视图类)
  • 通知属性示例:

    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;
    }
    
    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( ... );
    }
    
    依赖项属性示例:

    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;
    }
    
    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( ... );
    }
    

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

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

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

    然后在构造函数中

    public Assgn5()
    {           
        InitializeComponent();
    
        this.DataContext = this; // or **stbSQLConnectionString**.DataContext = this;
    }
    
     <TextBox 
       Name="stbSQLConnectionString" 
       Text="{Binding text}">
     </TextBox>
    
    public Assgn5()
    {           
    初始化组件();
    this.DataContext=this;//或**stbSQLConnectionString**.DataContext=this;
    }
    
    或将类实现InotifyProperty更改为自动绑定。我想您的意思是将其更改为DependencyProperty。在DependencyObject上实现INPC是愚蠢的。想想看,在这种情况下ElementName是什么?窗口的名称?@killianmcc:它就在代码示例中
    我知道这个问题是针对WPF的,但是如果您的目标是Silverlight,那么这就是解决方法。。因为Silverlight for Web和Phone没有用于RelativeSource的FindAncestor方法