我跑了一個試圖獲取我的BoundData在WPF中更新的問題。我的數據顯示,但我的用戶界面不響應數據的變化。如何正確使用DataBinding,INotifyPropertyChanged,ListViewGridView
我有一個XAML類看起來像這樣:
<Window x:Class="CSharpBoiler.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:util="clr-namespace:Wpf.Util"
Title="MainWindow" Loaded="Window_Loaded" Closing="Window_Closing" Height="Auto" Width="Auto"
DataContext="{Binding matchDataList, RelativeSource={RelativeSource Self}}">
<Grid>
<ListView
IsSynchronizedWithCurrentItem="True"
util:GridViewSort.AutoSort="True"
x:Name="MainListView" >
<ListView.View>
<GridView x:Name="MainGridView">
<GridView.Columns>
<GridViewColumn Header="Demo, press to Download"
util:GridViewSort.PropertyName="Demo"
x:Name="DemoGridViewColumn">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<ProgressBar x:Name="DemoButtonProgressbar"
Maximum="100" Minimum="0"
Value="{Binding AnalysisProgress,
UpdateSourceTrigger=PropertyChanged}" Width="100"/>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
</Grid>
MyMainWindow看起來是這樣的:
public partial class MainWindow : Window
{
private List<MatchData> matchDataList = new List<MatchData>();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MainListView.ItemsSource = matchDataList;
}
在matchDataList是一個數字MatchData的對象,我想代表我的ListViewGridView。 MatchData看起來像這樣:
public class MatchData : ObservableCollection<MatchData>, INotifyPropertyChanged
{
public int _AnalysisProgress;
public int AnalysisProgress
{
get { return _AnalysisProgress; }
set
{
_AnalysisProgress = value;
NotifyPropertyChanged("AnalysisProgress");
}
}
public PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
此時我的進度條顯示在我的ListGridViewgridView構造中。我在我的代碼中更改它的值以顯示進度。當我更改AnalysisProgress的值NotifyPropertyChanged被調用正確。但PropertyChanged始終爲空。
因此沒有事件被觸發,並且進度條的值沒有改變。當我通過單擊其中一個列標題刷新UI時,Progressbar顯示正確的值。顯然,我想讓我的進度條顯示當前的進度而不點擊它。
我對XAML Binding很陌生,希望我沒有犯過很多根本性的錯誤,因此我非常歡迎所有其他提案,以便更好地編寫代碼。我不太喜歡我在訪問ListViewGridview構造項時受到的限制,但它是我通過單擊列標題進行排序的最佳表格。
嘗試將列表更改爲ObservableCollection。 ObservableCollection matchDataList = new ObservableCollection () –
2015-02-06 22:19:41
@Ganesh更改它,PropertyChanged仍然爲空,UI不更新。 – Master117 2015-02-06 22:30:05
你的綁定存在一些問題。 – 2015-02-06 22:47:44