Pri*_*TSS 23 data-binding wpf binding
你能绑定到这样的局部变量吗?
SystemDataBase.cs
namespace WebWalker
{
public partial class SystemDataBase : Window
{
private string text = "testing";
...
Run Code Online (Sandbox Code Playgroud)
SystemDataBase.xaml
<TextBox
Name="stbSQLConnectionString"
Text="{SystemDataBase.text}">
</TextBox>
Run Code Online (Sandbox Code Playgroud)
??
文本设置为局部变量"text"
小智 36
模式是:
public string Text {get;set;}
Run Code Online (Sandbox Code Playgroud)
并且绑定是
{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}
Run Code Online (Sandbox Code Playgroud)
如果希望绑定自动更新,则应将其设为DependencyProperty.
我认为3.5添加ElementName到绑定,所以以下更容易:
<Window x:Name="Derp" ...
<TextBlock Text="{Binding Text, ElementName=Derp}"/>
Run Code Online (Sandbox Code Playgroud)
Dan*_*rod 25
要绑定到本地"变量",变量应该是:
例子:
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;
}
Run Code Online (Sandbox Code Playgroud)
Nei*_*eil 15
如果您正在做很多这样的事情,可以考虑将整个窗口的DataContext绑定到您的类.这将默认继承,但仍可像往常一样被覆盖
<Window DataContext="{Binding RelativeSource={RelativeSource Self}}">
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用单个组件
Text="{Binding Text}"
Run Code Online (Sandbox Code Playgroud)