2012-03-29 176 views
1

我加入的.csTextBlock绑定?

public static readonly DependencyProperty lbStatusProperty = 
     DependencyProperty.Register("lbStatus", typeof(string), typeof(SingleTalkView), 
     new PropertyMetadata("")); 

    public string lbStatus 
    { 
     get { return (string)GetValue(lbStatusProperty); } 
     set { SetValue(lbStatusProperty, value); } 
    } 

在XAML

<TextBlock Text="{Binding lbStatus}" Style="{StaticResource PhoneTextNormalStyle}" Height="24"/> 

然后代码添加一个全局值

private string a = "Test"; 

和初始化函数

this.lbStatus = a; 

决赛我添加一个按钮并更改a值,TextBlock不会改变!为什么? Thx ~~~~

回答

1

.NET中的字符串是不可变的类型。如果键入:

this.lbStatus = a; 

您设置lbStatus对当前由a变量指向的字符串的引用。后来,当你改变:

a = "Foo"; 

你不打算改变this.lbStatus,因为你分配a变量到一个完全新的字符串实例。

+0

哦......我如何结合TextBlock的? – Yagami 2012-03-29 05:38:44

+0

@Yagami通常,你会绑定到实现INotifyPropertyChanged的类的Property。你可以参考我关于WPF数据绑定的文章,这与Windows Phone中的绑定非常相似:http://reedcopsey.com/2009/11/25/better-user-and-developer-experiences-from-windows-形式对WPF与 - MVVM部分-4-数据绑定/ – 2012-03-29 05:42:01

0

这可以帮助你更好地了解

public class Base : INotifyPropertyChanged 
    {  
     public event PropertyChangedEventHandler PropertyChanged;  
     protected void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     }  
    } 

//ViewModel 
public class ViewModel : Base 
private string _value; 
     public string value { 
      get 
      { 
       return _value; 
      } 
      set 
      { 
       _value = value; 
       this.NotifyPropertyChanged("value"); 
      } 
     } 
//View 
<Textbox Height="60" Width="60" Foreground="Wheat" 
         Text="{Binding value,Mode=TwoWay}" >