2013-07-26 93 views
2

我有一個項目,其中我將TextBlock的Text屬性與代碼隱藏中的get/set綁定在一起。但是,當應用程序加載時,更改綁定屬性無法更新ui。C#WPF。綁定屬性更改,但多線程時UI不更新

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <ListBox SelectionMode="Multiple" x:Name="lstFileManager" HorizontalContentAlignment="Stretch" Background ="Transparent" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Visibility="{Binding ui_visiable}" Margin="5,0,0,0"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="Auto" /> 
          <RowDefinition Height="Auto" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="{Binding ui_complete_percentage}"/> 
          <ColumnDefinition Width="{Binding ui_incomplete_percentage}"/> 
         </Grid.ColumnDefinitions> 
         <Rectangle Opacity="0.8" Grid.RowSpan="2" Grid.Column="0" Fill="{Binding ui_complete_color}" /> 
         <Rectangle Opacity="0.8" Grid.RowSpan="2" Grid.Column="1" Fill="{Binding ui_incomplete_color}"/> 
         <TextBlock Margin="20,10,20,10" Text="{Binding ui_tip_text}" Grid.ColumnSpan="2" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" MinHeight="20" FontSize="12" FontFamily="Microsoft YaHei"/> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
      <ListBox.ItemContainerStyle> 
       <Style TargetType="ListBoxItem"> 
        <Setter Property="HorizontalAlignment" Value="Stretch"/> 
        <Setter Property="Margin" Value="0,0,0,1"/> 
       </Style> 
      </ListBox.ItemContainerStyle> 
     </ListBox> 
    </Grid> 
</Window> 

MainWindow.xaml.cs:

using System; 
using System.Collections.ObjectModel; 
using System.Threading; 
using System.Windows; 
using System.Windows.Threading; 

namespace WpfApplication1 { 
    /// <summary> 
    /// MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window { 

     public MainWindow() { 
      InitializeComponent(); 
      Bind(); 
      RefreshFileList(); 
     } 

     #region DataBind 
     public class FileListViewItem { 
      public string ui_visiable { get; set; } 
      public string ui_complete_percentage { get; set; } 
      public string ui_incomplete_percentage { get; set; } 
      public string ui_complete_color { get; set; } 
      public string ui_incomplete_color { get; set; } 
      public string ui_tip_text { get; set; } 
     } 
     ObservableCollection<FileListViewItem> FileListViewItemGroup = new ObservableCollection<FileListViewItem>(); 
     public void Bind() { 
      lstFileManager.ItemsSource = FileListViewItemGroup; 
     } 
     #endregion 

     #region Refresh File List 
     private void RefreshFileList() { 
      //new Thread((process) => { 
       Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { 
        FileListViewItemGroup.Clear(); 
       })); 
       for (int i = 0; i < 10; i++) { 
        FileListViewItem flvi = new FileListViewItem { 
         ui_tip_text = "1", 
         ui_visiable = "Visiable", 
         ui_complete_percentage = "0*", 
         ui_incomplete_percentage = "100*", 
         ui_complete_color = "White", 
         ui_incomplete_color = "#FFFF43FF" 
        }; 
        Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { 
         FileListViewItemGroup.Add(flvi); 
        })); 
       } 
       // those code below did not make sence when I am using mutli-threading. 
       FileListViewItemGroup[0].ui_complete_percentage = "100*"; 
       FileListViewItemGroup[0].ui_incomplete_percentage = "0*"; 
       FileListViewItemGroup[0].ui_tip_text = "what's wrong!"; 
      //}).Start(); 
     } 
     #endregion 

    } 
} 

的問題是,當我試圖使用多線程,這些代碼(FileListViewItemGroup [0] .ui_complete_percentage =「100 *」;)不起作用。

我整晚都在努力工作,我不知道爲什麼。 任何機構可以修復代碼或有一些解釋來幫助我? 謝謝。

+5

當它是單線程時它工作嗎?您需要讓UI知道屬性何時更改。爲FieldListViewItem實現'INotifyPropertyChanged'接口。有數千個關於如何實現和使用這個接口的例子。 – PoweredByOrange

+0

也可以查看'DependencyProperty'中的FileListViewItem類的屬性。 – Nick

+0

是的,它是單線程的。我正在搜索實現INotifyPropertyChanged接口的示例。謝謝。(我是學習WPF的初學者。:-) – tinymins

回答

0

(問題解決了評價和從OP問題編輯改建成爲一個社區維基答案見Question with no answers, but issue solved in the comments (or extended in chat)。)

的OP寫道:

問題解決了,這裏是我的代碼:

#region DataBind 
    public class FileListViewItem : INotifyPropertyChanged 
    { 
     public string ui_visiable { get; set; } 
     public string ui_complete_percentage { get; set; } 
     public string ui_incomplete_percentage { get; set; } 
     public string ui_complete_color { get; set; } 
     public string ui_incomplete_color { get; set; } 
     private string _ui_tip_text; 
     public string ui_tip_text 
     { 
      get { return _ui_tip_text; } 
      set 
      { 
       _ui_tip_text = value; 
       RaisePropertyChanged("ui_tip_text"); 
      } 
     } 

     #region Implement INotifyPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 
     public void RaisePropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     #endregion 
    } 
    ObservableCollection<FileListViewItem> FileListViewItemGroup = new ObservableCollection<FileListViewItem>(); 
    public void Bind() { 
     lstFileManager.ItemsSource = FileListViewItemGroup; 
    } 
    #endregion 

感謝PoweredByOrange和尼克提供的幫助:)