jerry jose
jerry jose

Reputation: 11

Using progress bar to upload a file in WPF C# MVVM

I want to show the progress bar on uploading a file

xaml

  <ProgressBar HorizontalAlignment="Left" Height="30" Margin="145,6,0,0" VerticalAlignment="Top" Width="469"   Value="{Binding progressBar1,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"   Grid.Row="1" />

This is my progress control

model.cs

public string progressBar1
        {
            get
            {
                return _ProgressBar;
            }
            set
            {
                _ProgressBar = value;
                OnPropertyChanged("progressBar1");
            }
        }

ViewModel

  private Files _FileDetails;

        public Files FilesDetails
        {
            get
            {
                return _FileDetails;
            }
            set
            {
                _FileDetails = value;
            }
        }

//some code 

 void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            FilesDetails.progressBar1.Value = e.ProgressPercentage;

            if ((progressBar1.Value / 2) == 0)
            {
                lblStatus.Content = "Downloading.";
            }
            else if ((progressBar1.Value / 3) == 0)
            {
                lblStatus.Content = "Downloading..";
            }
            else if ((progressBar1.Value / 5) == 0)
            {
                lblStatus.Content = "Downloading...";
            }

            lblStatus.Content = "Download " + filesizedownloaded.ToString("F2") + " / " + filesize.ToString("F2")
                + " ( " + progressBar1.Value.ToString() + " ) % Complete.";
        }

in worker_ProgressChanged function i cant access the value of progressBar1.Shows error under "value"

string' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

shows the above error on hover the value

Upvotes: 0

Views: 1149

Answers (2)

Andy
Andy

Reputation: 12276

When you use a numeric variable, you don't stick a .Value on the end when you reference it.

And your ,UpdateSourceTrigger=PropertyChanged in that binding is inappropriate. Remove it. There's also no point in making the Progressbar ValueProperty bind one way anyhow so make your binding simpler:

Value="{Binding progressBar1}"

Then take a look at your code.

And think about what it's doing.

That property, can be a double or int but let's go with int. So the backer also needs changing.

    private int _ProgressBar = 0;
    public int progressBar1
    {
        get =>_ProgressBar;
        set
        {
            _ProgressBar = value;
            OnPropertyChanged("progressBar1");
        }
    }

Removing all those .Value gives:

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1 = e.ProgressPercentage;

        if ((progressBar1 / 2) == 0)
        {
            lblStatus.Content = "Downloading.";
        }
        else if ((progressBar1 / 3) == 0)
        {
            lblStatus.Content = "Downloading..";
        }
        else if ((progressBar1 / 5) == 0)
        {
            lblStatus.Content = "Downloading...";
        }

        lblStatus.Content = $"Download {progressBar1} % Complete.";
    }

I see no definition for filesize so I removed that. It's not clear where that worker_ProgressChanged handler is and hence where the progressBar1 property is in relation to it. I'm guessing in the same class for the above code.

Notes:

If you're telling us you have an error.

Tell us which line and make sure your code in the question matches what you're using.

Read up on naming conventions.

Most teams use public starts UpperCase

private starts lowerCase

There is a fair bit of variation on prefixing with underscores.

Upvotes: 1

IndieGameDev
IndieGameDev

Reputation: 2974

You need to make progressBar1 and integer.

public int progressBar1
{
     .....
}

After that you can access it's value with just the name and because it's an integer you can also divide it by the values you've assigned.

Make sure to add the last part of the message with += tough so the message we just put into the string (eg. "Downloading.") won't be immediately overwritten.

progressBar1 = e.ProgressPercentage;

if ((progressBar1 / 2) == 0) {
    lblStatus.Content = "Downloading.";
}

.....

lblStatus.Content += filesizedownloaded.ToString("F2") + " / " + filesize.ToString("F2") 
    + " ( " + progressBar1.Value.ToString() + " ) % Complete.";

Upvotes: 0

Related Questions