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

查看:269
本文介绍了WPF绑定到局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以绑定到这样的局部变量吗?



SystemDataBase.cs

 命名空间WebWalker 
{
public partial class SystemDataBase:Window
{
private string text =testing;
...

SystemDataBase.xaml

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

??



文字设置为局部变量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}/>


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"

解决方案

The pattern is:

public string Text {get;set;}

and the binding is

{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}

If you want the binding to update automatically you should make it a DependencyProperty.


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屋!

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