2011-09-03 50 views
0
foreach (var t in ((App)App.Current).CollectionMessages) 
    if (t.Uid.ToString() == uid) 
    t.TexT = z; 

在CollectionMessages項,申請文本更改文本到我想要的東西。但UI沒有變化,它顯示了舊的價值。怎麼了?請幫助我。如何更新UI中的ObservableCollection?

的XAML:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <StackPanel Margin="0,0,0,0"> 
     <TextBlock Text="Диалоги" Style="{StaticResource PhoneTextNormalStyle}" FontSize="{StaticResource PhoneFontSizeLarge}"/> 
    </StackPanel> 
<Grid Grid.Row="2"> 
       <Grid d:LayoutOverrides="GridBox" > 
        <ListBox x:Name="mess_list" ItemsSource="{Binding}" > 
         <ListBox.ItemTemplate> 
          <DataTemplate > 
           <Grid> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="Auto"/> 
             <ColumnDefinition Width="*"/> 
            </Grid.ColumnDefinitions> 

             <TextBlock Text="{Binding TexT}" " /> 

...

回答

3

你需要確保它的t是一種類實現INotifyPropertyChangedMSDN),而當你設置TexT觸發該事件PropertyChanged

3

您需要實現INotifyPropertyChanged接口等,該UI知道它必須更新提出一個屬性更改事件。

How to:頁面有一個工作的例子。你基本上需要這樣的代碼:

public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

當你想要更新的東西被改變時被調用。因此,對於TexT代碼變爲:

private string localTexT 
public string TexT 
{ 
    get { return localTexT; } 
    set 
    { 
     localTexT = value; 
     NotifyPropertyChanged("TexT"); 
    } 
} 

從您的更新,它看起來像你有ItemsSourceTextBlock正確的結合。但是,您是否設置了視圖的DataContext

+0

NotifyPropertyChanged被解僱了,但沒有在UI改變。 – SevenDays

+0

@wsevendays - 需要看到的XAML。你在使用MVVM嗎? – ChrisF

+0

不,我正在使用ItemsSource屬性。 – SevenDays

相關問題