2012-01-21 101 views
0

的炫魅:爲什麼PropertyChangedEventHandler爲空?

MainPage.xaml中

<Canvas x:Name="LayoutRoot" Background="White"> 
</Canvas> 

MainPage.xaml.cs中

List<Usol> list = new List<Usol>(); 
for (int i = 0; i < 10; i++) 
{ 
    var element = new Usol(); 
    list.Add(element); 
    Canvas.SetTop(element, i * 25); 
    LayoutRoot.Children.Add(list[i]); 
    } 
foreach (var item in list) 
{ 
    item.context.name = "Varken"; 
} 

一個用戶控件

Usol.xaml

<Grid x:Name="LayoutRoot" Background="White"> 
    <TextBlock Text="{Binding Name}" /> 
</Grid> 

Usol.xaml.cs

public Context context; 
public Usol() 
{ 
    InitializeComponent(); 
    context = new Context(); 
    this.DataContext = context; 
} 

一類

Context.cs

public class Context : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    }  
    #region Fields 
    /// <summary> 
    /// Field Declaration for the <see cref="Name"/> 
    /// </summary> 
    private string name; 
    #endregion 
    #region Properties 
    /// <summary> 
    /// Gets or Sets the Name 
    /// </summary> 
    public string Name 
    { 
     get { return name; } 
     set 
     { 
      if (this.name != value) 
      { 
       this.name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 
    #endregion 
} 

現狀

我創造了這個小測試應用程序在一個更大的應用程序來複制一個問題,我有。它以相同的方式工作(不完全,但足夠接近)。

它增加了一些定製的用戶控件和每個獲得自己的一個DataContext類的實例。

然而,沒有任何屬性都願意更新themselfs由於空PropertyChangedEventHandler

問題

爲什麼public event PropertyChangedEventHandler PropertyChanged;總是空?

+0

只是一個建議。您沒有遵循預期的字段/屬性名稱約定。 –

+0

你的意思是沒有人訂閱?如果您使用OneTime綁定,這是我期望的行爲。 。如果你沒有定義接口(例如事件在那裏,但類定義它),也會發生這種情況。 –

回答

1

你的代碼是錯誤的,

OnPropertyChanged("Name"); <-- should update "name" not "Name" 

你是點火事件說,「姓名」改變,但屬性名稱是「名」,C#和約束力是區分大小寫的。

將其更改爲,在病房

#region Fields 
/// <summary> 
/// Field Declaration for the <see cref="name"/> 
/// </summary> 
private string _Name; 
#endregion 
#region Properties 
/// <summary> 
/// Gets or Sets the name 
/// </summary> 
public string Name 
{ 
    get { return _Name; } 
    set 
    { 
     if (this._Name != value) 
     { 
      this._Name = value; 
      OnPropertyChanged("Name"); 
     } 
    } 
} 
#endregion 

從C#6,請使用nameof()關鍵字...

#region Fields 
/// <summary> 
/// Field Declaration for the <see cref="name"/> 
/// </summary> 
private string _Name; 
#endregion 
#region Properties 
/// <summary> 
/// Gets or Sets the name 
/// </summary> 
public string Name 
{ 
    get { return _Name; } 
    set 
    { 
     if (this._Name != value) 
     { 
      this._Name = value; 
      OnPropertyChanged(nameof(Name)); 
     } 
    } 
} 
#endregion 
+0

固定錯字:此應用程序中存在固定問題。然而,在我的「更大」的應用程序中,我仍然有類似的問題,我確信綁定已正確完成。需要一些時間來進一步調查我的問題 –

1

Context.cs需要實現INotifyPropertyChanged接口。你在做那個嗎?

編輯:張貼您的更新。

當程序員創建Model/ViewModel的「兩個」實例時,我通常會看到這種問題。當您使用View附加一個實例時,它始終是另一個獲取更新(哪個課程將具有null PropertyChanged訂閱者)。因此,您必須確保您的視圖正在使用與其他部分更新相同的實例。 希望我的觀點清楚。

+0

公共類上下文:INotifyPropertyChanged。是的,用適當的信息更新了問題。謝謝你指出我忘了在我的問題中加上這個。 –

+0

我在網上遇到了一些這樣的答案,我理解這個問題。但我不明白的是silverlight如何能夠期待2個實例。即使這樣只有一個實例可以用於綁定,至少有一個PropertyChanged應該有一個值,因爲所有正在創建的10個綁定到一個視圖。 –

相關問題