如何正确使用DataBinding,INotifyPropertyChanged,ListViewGridView

时间:2015-02-06 22:12:30

标签: c# wpf xaml listview inotifypropertychanged

我试图在WPF中更新我的BoundData。我的数据已显示,但我的用户界面并未对数据中的更改做出反应。

我的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始终为null。

因此,不会触发任何事件,并且Progressbar的值不会更改。当我通过单击其中一个列标题刷新UI时,Progressbar显示正确的值。显然,我想让我的Progressbar显示当前进度而不点击它。

我对XAML Binding很陌生,并希望我没有犯很多根本性错误,因此我对如何使这些代码更好的所有其他提议持开放态度。我不太喜欢我访问ListViewGridview构造项的限制,但它是我找到的最好的表,它通过单击列标题进行排序。

Image of the Running Programm, Progressbar is to the right of the Analyse Button

1 个答案:

答案 0 :(得分:0)

问题出在

public class MatchData : ObservableCollection<MatchData>, INotifyPropertyChanged

将其更改为

public class MatchData : INotifyPropertyChanged
在这样做并修复

后,VS让我实现了接口
    public PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

        public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }

    public int _AnalysisProgress;
public int AnalysisProgress
{
    get { return _AnalysisProgress; }
    set
    {
        _AnalysisProgress = value;
        NotifyPropertyChanged("AnalysisProgress");
    }
}

        public int _AnalysisProgress { get; set; }
    public int AnalysisProgress
    {
        get { return _AnalysisProgress; }
        set
        {
            _AnalysisProgress = value;
            OnPropertyChanged(new PropertyChangedEventArgs("AnalysisProgress"));
        }
    }

一切都在按预期运行。