WPF绑定到局部变量 [英] WPF Binding to local variable

查看:791
本文介绍了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 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 Name="Derp" ...
  <TextBlock Text="{Binding Text, ElementName=Derp}"/>

这篇关于WPF绑定到局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆