2013-08-26 51 views
0

我在我的應用程序中的問題,它不能正確更改自定義屬性後更新UI,即綁定使用DisplayMemberBinding="{Binding property}"一個GridView列。WPF定時器和UI更新問題

XAML:

<ListView x:Name="downloadList" HorizontalAlignment="Left" Height="293" Margin="0,126,0,0" VerticalAlignment="Top" Width="810" Grid.IsSharedSizeScope="True" MouseDoubleClick="DownloadList_MouseDoubleClick"> 
    <ListView.View> 
     <GridView x:Name="DownloadGridView"> 
      <GridViewColumn x:Name="c_filename" Header="File name" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_fileName_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding fileName}" /> 
      <GridViewColumn x:Name="c_size" Header="Size" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_size_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding formattedFileSize}" /> 
      <GridViewColumn x:Name="c_downloaded" Header="Downloaded" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_downloaded_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding sizeProgress}" /> 
      <GridViewColumn x:Name="c_status" Header="Status" Width="{Binding Source={x:Static p:Settings.Default}, Path=downloadList_status_Width, Mode=TwoWay}" DisplayMemberBinding="{Binding Status}"/> 
     </GridView> 
    </ListView.View> 
</ListView> 

這是我的自定義類屬性:

using System; 
using System.Runtime.CompilerServices; 
using System.Text; 
using System.ComponentModel; 

namespace DownloadManager 
{ 
public class DownloadItem : INotifyPropertyChanged 
{ 
    private string _filepath; 
    public string filePath 
    { 
     get { return _filepath; } 
     set 
     { 
      _filepath = value; 
      RaisePropertyChanged(); 
     } 
    } 

    private int _sizeprogress; 
    public int sizeProgress 
    { 
     get { return _sizeprogress; } 
     set 
     { 
      _sizeprogress = value; 
      RaisePropertyChanged(); 
     } 
    } 

// and so on... 

    public event PropertyChangedEventHandler PropertyChanged; 
    public void RaisePropertyChanged(
     [CallerMemberName] string caller = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(caller)); 
     } 
    } 

} 
} 

定時器:編輯,以顯示我想要做的

一個真實的例子
System.Windows.Threading.DispatcherTimer updateTimer = new System.Windows.Threading.DispatcherTimer(); 

updateTimer.Tick += new EventHandler(updateTimer_Tick); 
updateTimer.Interval = new TimeSpan(0, 0, 1); 


private void updateTimer_Tick(object sender, EventArgs e) 
{ 
    foreach (DownloadItem item in downloadList.Items) 
    { 
     long BytesReceived = item.filePath.Length; 
     item.sizeProgress = BytesReceived; 
    } 
} 

item.filePath包含文件的路徑正在下載,使用FileStream來編寫它。

我的目標是每秒鐘讀取文件大小並顯示它。

問題:的UI,在這種情況下,列綁定到sizeProgress,正在更新,只有一次,只在第一跳,然後什麼都沒有。該應用程序仍然運行沒有任何例外..

而我真的不知道可能是什麼問題。

如果您需要更多信息/密碼告訴我。謝謝。

+1

你是否嘗試過將一個實際的集合綁定到'ListView'而不是直接操縱'ListView.Items'。綁定一個'ObservableCollection '可能表現得更加可靠 –

+0

@ sa_ddam213不,我會看看你的建議,但我不認爲這是問題所在。我有其他的功能,更新列表視圖內的項目,他們確實工作(UI更新也)。 – Fr0z3n

+0

你有沒有在tick處理程序中插入一個斷點?處理程序是否被多次調用?是否有一個異常被拋出另一個線程搞亂了事情(VS中的ctrl-alt-e,檢查「Common Language Runtime Exceptions:Thrown」)? – canton7

回答

1
long BytesReceived = item.filePath.Length; 

呃,那是string的包含文件的路徑,而不是文件本身的長度的長度。