WPF为什么我的ObservableCollection对象集合没有更新我的ProgressBar

时间:2015-06-11 10:28:21

标签: wpf listview progress-bar

我有WPF application我正在使用PcapDotNet DLLs通过packets发送Networkcard

这是我的代表Wireshark file的对象:

public class WiresharkFile
{
    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 ObservableCollection<WireshrkFile> files{ get; set; }

正如您在每个文件中看到的那样,我正在计算Progress

现在我的所有文件都在ListView内,而ListView我有Progress-Bar column

这是我的Progress-Bar风格:

<!-- Progress-Bar style -->
<Style x:Key="CustomProgressBar" TargetType="ProgressBar" >
    <Setter Property="Template" >
        <Setter.Value>
            <ControlTemplate TargetType="ProgressBar">
                <Border BorderBrush="Transparent" BorderThickness="1" Background="LightGray" CornerRadius="0" Padding="0" >
                    <Grid x:Name="PART_Track">
                        <Rectangle x:Name="PART_Indicator" HorizontalAlignment="Left" Fill="#FF15669E" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我的ListView

中的进度条定义
<ListView.Resources>
    <DataTemplate x:Key="MyDataTemplate">
        <Grid Margin="-6">
            <ProgressBar Name="prog" Maximum="100" Value="{Binding Progress}" 
                         Width="{Binding Path=Width, ElementName=ProgressCell}" 
                         Height="20" Margin="0" Background="#FFD3D0D0" Style="{StaticResource CustomProgressBar}" />
            <TextBlock Text="{Binding Path=Value, ElementName=prog, StringFormat={}{0}%}" VerticalAlignment="Center"
                       HorizontalAlignment="Center" FontSize="11" Foreground="Black" />
        </Grid>
    </DataTemplate>
    <ControlTemplate x:Key="ProgressBarTemplate">
        <Label  HorizontalAlignment="Center" VerticalAlignment="Center" />
    </ControlTemplate>
</ListView.Resources>

因此,当Wireshark file正在进行中时,我可以看到它发送packets并且属性发生了变化,但Propress-Bar仍然在0%。 所以我做了一点测试,在timer event click里面改变了我的WireshakFile Progress属性,在这种情况下我可以看到我ListView Progress-Bar的变化。 所以我的问题是我做错了什么?

修改

我也尝试了一些看起来非常奇怪的东西,但这是在讨论:

在我的Timer_Tick内,我尝试遍历我的ObservableCollection集合:

foreach (WiresharkFile item in files)
{
    item.Progress = item.Progress;
}

正如你所看到的,它只是指向同一属性,但这是有效的。

0 个答案:

没有答案