c# - 如何正确使用DataBinding、INotifyPropertyChanged、ListViewGridView

标签 c# wpf xaml listview inotifypropertychanged

我遇到了一个问题,试图在 WPF 中更新我的 BoundData。我的数据已显示,但我的 UI 未对数据中的更改使用react。

我有一个如下所示的 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 中有许多我想在我的 ListViewGridView 中表示的 MatchData 对象。 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));
    }

}

此时我的 Progressbar 显示在我的 ListGridViewgridView 构造中。我在我的代码中更改它的值以显示进度。当我更改 AnalysisProgress 的值时,NotifyPropertyChanged 被正确调用。但 PropertyChanged 始终为空。

因此没有事件被激发,Progressbar 的值也没有改变。当我通过单击列标题之一刷新 UI 时,Progressbar 显示正确的值。显然我想让我的 Progressbar 显示当前进度而无需单击它。

我是 XAML 绑定(bind)的新手,希望我没有犯很多基本错误,因此我对所有其他关于如何改进此代码的建议持开放态度。我不太喜欢我在访问 ListViewGridview 构造的项目时受到的限制,但它是我发现的最好的表格,它通过单击列标题进行排序。

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

最佳答案

问题出在

public class MatchData : ObservableCollection<MatchData>, INotifyPropertyChanged

改成之后

public class MatchData : INotifyPropertyChanged

在这样做并修复之后,VS 让我实现接口(interface)

    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"));
        }
    }

一切正常。

关于c# - 如何正确使用DataBinding、INotifyPropertyChanged、ListViewGridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28375701/

相关文章:

c# - 依赖关系未显示在应用程序 map 中?

c# - 何时评估 XAML 绑定(bind)?

c# - 实现不必要的接口(interface)成员或创建新接口(interface)?

c# - 从流创建 FormFile

c# - 单击它更改内容并返回原始状态的按钮

.net - 如何最好地处理 WPF 单选按钮?

c# - BitmapSource.Create 错误 - 缓冲区大小不够大

c# - 如何在单击按钮期间更改 TextBlock 文本?

windows - 如何更改针对 Windows Phone 8.1 (WinRT) 更改的文本的 TextBlock 前景色?

c# - 地铁应用程序中的 ComboBox 如何获取所选文本及其相关 ID