WPF绑定到本地变量

29

你能像这样绑定到本地变量吗?

SystemDataBase.cs

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

SystemDataBase.xaml

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

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

5个回答

42

格式为:

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}"/>

3
或者让该类实现INotifyPropertyChanged接口,以使绑定自动工作。 - Lars Truijens
1
我想你的意思是将其更改为DependencyProperty。在DependencyObject上实现INPC是愚蠢的。 - user1228
想一想,在这种情况下,ElementName是什么?窗口的名称吗? - windowsgm
2
@killianmcc:代码示例中就有。<Window Name="Derp" 其中Derp是(至少是我通常这样做的)定义在窗口类上的DependencyProperty(public partial class Herp : Window {})。 - user1228

29

为了将变量绑定到本地,变量应该是:

  1. 一个属性,而不是一个字段。
  2. 公共的。
  3. 要么是通知属性(适用于模型类),要么是依赖属性(适用于视图类)。

通知属性示例:

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

20
如果你需要经常这样做,可以考虑将整个窗口的DataContext绑定到你的类。默认情况下,它将被继承,但仍然可以像往常一样被覆盖。
<Window DataContext="{Binding RelativeSource={RelativeSource Self}}">

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

Text="{Binding Text}"

我知道这个问题是关于WPF的,但如果你的目标是Silverlight,那么这就是正确的方法。因为Silverlight用于Web和Phone的版本没有RelativeSource的FindAncestor方法。 - Adarsha

3
要绑定 Window 类中存在的本地变量,需要满足以下条件: 1. 公共属性。 2. 一个通知属性。为此,您的窗口类应该为此属性实现 INotifyPropertyChanged 接口。
然后在构造函数中:
public Assgn5()
{           
    InitializeComponent();

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

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

0
需要在int构造函数中添加以下行:
this.DataContext = this;

并在XAML中使用:

<TextBox Text = "{Binding SomeProperty}" />

例子:

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public string PersonName { get; set; }

    public MainWindow()
    {
        InitializeComponent();            
        PersonName = "default";
        this.DataContext = this;
    }

    void button1_Click(object sender, RoutedEventArgs args)
    {            
        MessageBox.Show($"Hello {PersonName}");
    }
}

MainWindow.xaml

<StackPanel>
        <TextBox Name="textbox1" 
                 Text="{Binding PersonName, Mode=TwoWay}"
                 />
        <Button Name="button1" Click="button1_Click" Content="Click Me" />
</StackPanel>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接