0

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

8
  • You are accessing your progressBar1 from model with progressBar1.Value which is a string and doesn't have a reference to Value. You probably meant FilesDetails.progressBar1.Value Commented Oct 9, 2020 at 12:15
  • 1
    FilesDetails.progressBar1.Value = e.ProgressPercentage; i have tried this and error shown for "value". The error message have added in my question Commented Oct 9, 2020 at 12:18
  • 1
    all progressBar1.Value shows error. error indicator under value Commented Oct 9, 2020 at 12:21
  • 1
    i think when i write "FilesDetails.progressBar1" it consider it as a string.so i cant use value there.but i want to assign a value so that i have to show the percentage Commented Oct 9, 2020 at 12:23
  • 1
    declear " public int progressBar1=0;" at the top ..but shows "Operator '/' cannot be applied to operands of type 'string' and 'int' " Commented Oct 9, 2020 at 12:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.