2014-06-21 57 views
0

我有一個類,從BindableBase,其中包含兩個屬性得出:XAML GridView控件,並的ObservableCollection改變項目的屬性

private string sourceCallNumber; 
    public string SourceCallNumber 
    { 
     get { return sourceCallNumber; } 
     set { SetProperty(ref sourceCallNumber, value); } 
    } 

    private string sourceMediaType; 
    public string SourceMediaType 
    { 
     get { return sourceMediaType; } 
     set { SetProperty(ref sourceMediaType, value); } 
    } 

我有一個包含一些使用類項目的一個ObservableCollection。

我有一個GridView爲其設置ItemsSource指向ObservableCollection。

我的問題是,如果我改變的,比如說,SourceMediaType上的項目的一個值,顯示不更新。我已經把調試,並可以確認更改值導致OnPropertyChanged觸發該屬性。

我讀過周圍類似的問題相當多的SO問題和答案,我得到的是什麼,我需要爲了得到這個工作做的相當困惑。

我的理解是,雖然本身的ObservableCollection當一個屬性更改不會做任何事情,如果項目本身觸發OnPropertyChanged,應該得到更新顯示。

(我讀到的一個答案提出使用提供的代碼叫做TrulyObservableCollection,但是我得到的問題是所有內容都刷新了,而不僅僅是一個已更新的項目)。

請問我有什麼遺漏或誤解?

謝謝。

+0

你解釋的方式沒有錯。你確定綁定不是一次性嗎?你可以添加Xaml代碼嗎? – Reza

+0

我會在這裏進行一次瘋狂的嘗試,在你的SetProperty函數中,你不應該傳遞一個字符串來代表被更改的實際屬性嗎?除非你從字段名稱中獲得屬性名稱(這將需要所有的都遵循相同的模式)。 –

+0

@Reza - 不,綁定不是OneTime。 –

回答

0

C#應用程序應該實現INotifyCollectionChanged和System.Collections.IList(不是IList Of T)。

public class NameList : ObservableCollection<PersonName> 
{ 
    public NameList() : base() 
    { 
     Add(new PersonName("Willa", "Cather")); 
     Add(new PersonName("Isak", "Dinesen")); 
     Add(new PersonName("Victor", "Hugo")); 
     Add(new PersonName("Jules", "Verne")); 
    } 
    } 

    public class PersonName 
    { 
     private string firstName; 
     private string lastName; 

     public PersonName(string first, string last) 
     { 
      firstName = first; 
      lastName = last; 
     } 

     public string FirstName 
     { 
      get { return firstName; } 
      set { firstName = value; } 
     } 

     public string LastName 
     { 
      get { return lastName; } 
      set { lastName = value; } 
     } 
    } 

看看GridView

+0

儘管您需要實施INotifyCollectionChanged正確,但ObservableCollection 確實可以做到這一點。參考:http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh464965.Aspx(將控件綁定到對象集合下)。 –

0

@RodrigoSilva把我正確的道路上......引用值是這樣的XAML:

  <StackPanel> 
       <TextBlock Text="{Binding DisplayCallNumber}" Style="{StaticResource TitleTextBlockStyle}" Visibility="{Binding GotCallNumber, Converter={StaticResource DisplayIfTrue}}" Margin="0,0,0,10"/> 
       <TextBlock Text="{Binding DisplayMediaType}" Style="{StaticResource ItemTextStyle}" Visibility="{Binding GotMediaType, Converter={StaticResource DisplayIfTrue}}" Margin="0,0,0,10"/> 
      </StackPanel> 

不直接引用的基本性質SourceCallNumber和SourceMediaType。因此,儘管OnPropertyChanged對SourceCallNumber和SourceMediaType正確觸發,但不會導致顯示更新,因爲這不是XAML指向的內容。

明確改變調用的setProperty這樣的:

SetProperty(ref sourceCallNumber, value, "DisplayCallNumber"); 

解決了這個問題,但不是一個很好的修復,因爲應用程序的其他部分可能實際上被結合SourceCallNumber並不會得到一個屬性更新在這個變化之後。 GOOD修復方法是使用http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx中所述的轉換器。

相關問題