Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf ObservableCollection未更新我的UI_Wpf_Listview - Fatal编程技术网

Wpf ObservableCollection未更新我的UI

Wpf ObservableCollection未更新我的UI,wpf,listview,Wpf,Listview,这是我的模型: public class WiresharkFile : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; virtual public void NotifyPropertyChange(string propertyName) { var handler = PropertyChanged; if (ha

这是我的模型:

public class WiresharkFile : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    virtual public void NotifyPropertyChange(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

   private string _file; // file path
   private string _packets; // how many packet in file
   private string _sentPackets; // how many packet sent
   private string _progress; // percentage (_sentPackets/_packets) * 100

   public int Progress
   {
       get { return _progress; }
       set
       {
           _progress = value;
           NotifyPropertyChange("Progress");
       }
   }

   public void Transmit(WireshrkFile)
   {
         // here i am send the packets
   }

    public event PropertyChangedEventHandler PropertyChanged;

    virtual public void NotifyPropertyChange(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
我的模型集:

public ObservableCollection<WireshrkFile> files{ get; set; }

我可以看到我的集合属性正在更改,但mu
UI
没有更改。

注意:请注意:
ItemsSource=“{Binding WiresharkFile}”

将此更改为
ItemsSource=“{Binding files}”

我准备了一个小样本:

Window x:Class="ProgressBarChangedStack.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>
    <StackPanel>
        <ListView ItemsSource="{Binding files}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="Progress: "/>
                        <ProgressBar Value="{Binding Progress}" Margin="5" MinWidth="100"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button Content="Click to complete progress" Width="150" Margin="10" Click="btnProgressComplete_Click"/>
    </StackPanel>
</Grid>
我认为这里没有什么要解释的,你有同样的方法

这是我的测试代码:

 public partial class MainWindow : Window
{
    public ObservableCollection<Model> files { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        files = new ObservableCollection<Model>();
        files.Add(new Model() { Progress = 20 });
        files.Add(new Model() { Progress = 30 });
        files.Add(new Model() { Progress = 40 });
        this.DataContext = this;
    }

    private void btnProgressComplete_Click(object sender, RoutedEventArgs e)
    {
        foreach (var file in files)
        {
            file.Progress = 100;
        }
    }
}

使用它们的实际形式,发生更改时不会通知视图

您没有为所有WiresharkFile属性实现NotifyPropertyChange吗?顺便说一句,将它们从普通字段转换为公共属性。是的,我做到了,请看我的更新。如果你说你的UI没有更新,那么你说的只是提供进度条的进度属性?这个INPC实现了什么?我每一套都有NotifyPropertyChange,我需要一些elae吗?我还添加了ListView.Resources和我的进度bar@VerintVerint对于_文件,_数据包和_sentPackets所做的工作与您为进度所做的相同。将它们实现为公共属性,并在集合中添加NotifyPropertyChange。我完成了所有操作,但仍然看不到我的收藏更新了我的UI,我用另一个线程打开了工作,因此我的UI没有冻结,但这仍然不起作用,可能是什么问题?我刚刚看到你的btnProgressComplete\u单击,你为什么这样做?ObservableCollection无法自动更新我的UI?@VerintVerint ItemsSource=“{Binding WiresharkFile}”,我认为这里有一个错误。您可能希望将ItemsSource设置为文件集合。
public class Model : INotifyPropertyChanged
{
    private int _Progress;

    public int Progress
    {
        get { return _Progress; }
        set
        {
            _Progress = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Progress"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
}
 public partial class MainWindow : Window
{
    public ObservableCollection<Model> files { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        files = new ObservableCollection<Model>();
        files.Add(new Model() { Progress = 20 });
        files.Add(new Model() { Progress = 30 });
        files.Add(new Model() { Progress = 40 });
        this.DataContext = this;
    }

    private void btnProgressComplete_Click(object sender, RoutedEventArgs e)
    {
        foreach (var file in files)
        {
            file.Progress = 100;
        }
    }
}
  private string _file; // file path
  private string _packets; // how many packet in file
  private string _sentPackets; // how many packet sent
  private string _progress; // percentage (_sentPackets/_packets) * 100