WPF 绑定到局部变量 [英] WPF Binding to local variable
本文介绍了WPF 绑定到局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你能像这样绑定到局部变量吗?
Can you bind to a local variable like this?
SystemDataBase.cs
namespace WebWalker
{
public partial class SystemDataBase : Window
{
private string text = "testing";
...
SystemDataBase.xaml
<TextBox
Name="stbSQLConnectionString"
Text="{SystemDataBase.text}">
</TextBox>
??
Text 被设置为局部变量text"
Text is set to the local variable "text"
推荐答案
模式是:
public string Text {get;set;}
并且绑定是
{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}
如果您希望绑定自动更新,您应该将其设为 DependencyProperty.
If you want the binding to update automatically you should make it a DependencyProperty.
我认为 3.5 将 ElementName
添加到绑定中,因此以下内容更容易一些:
I think 3.5 added ElementName
to bindings, so the following is a little easier:
<Window x:Name="Derp" ...
<TextBlock Text="{Binding Text, ElementName=Derp}"/>
这篇关于WPF 绑定到局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文