WPF why my ObservableCollection object collection not update my ProgressBar

315 views Asked by At

i have WPF application and i am using PcapDotNet DLLs to send packets through the Networkcard.

This is my object that represent 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
   }
}

My list that hold this objects:

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

As you can see in every file that in process i am calculate the Progress.

Now all my files is inside ListView and in this ListView i have Progress-Bar column.

This is my Progress-Bar style:

<!-- 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>

Progress-Bar definition inside my 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>

So when the Wireshark file is in process i can see that it send the packets and the properties changing but the Propress-Bar is still with 0%. So i made i little test and inside timer event click changed my WireshakFile Progress property and in this case i can see the my ListView Progress-Bar changing. So my question is what i am doing wrong ?

Edit:

I also try something that looks very strange but this is woking:

Inside my Timer_Tick i try to loop over my ObservableCollection collection:

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

As you can see it only make pointer to the same property but this is working.

0

There are 0 answers