WPF percentage status shown in label MVVM

edited September 2015 in C#

Hi All Experts,

I got some problem in showing download percentage in GridView of WCF.i used MVVM pattern. here is my background worker in application start.

`public partial class MainWindow : Window
{
public MainWindow()
{
Overall.EverythingOk = "Nothing";

       InitializeComponent();
       //IRepo repo = new Repo();
       ViewModel.MainWindowsViewModel viewModel = new ViewModel.MainWindowsViewModel();
       this.DataContext = viewModel;

       BackGroundThread bgT = new BackGroundThread();
       bgT.bgWrk.RunWorkerAsync();
   }

}
`
Here is the DoWork function in BackGroundTHread class

public void bw_DoWork(object sender, DoWorkEventArgs e)
       {
           if (!Overall.stopStatus)
           {
               for (int i=0; i < 10000; i++)
               {
                   Overall.PercentageDwnd = i;
                   Overall.caseRefId = "999999";
                   if (i == 9998)
                   {
                       i = 1;
                   }
               }
           }
       }

Overall.PercentageDwnd and Overall.caseRefId are static variable(you can call from everywhere in the application) and always update until the backgroundworker completed. I got another viewmodel called TestViewModel and here it is.

public class TestViewModel:BindableBase
    {        
        private String _UpdatePer=Overall.PercentageDwnd.ToString();
        public String UpdatePercentage
        {
            get { return _UpdatePer; }
            set { SetProperty(ref _UpdatePer, value); }
        }

        private ObservableCollection _ViewAKA = new ObservableCollection();
        private tblTransaction model;

        public TestViewModel(tblTransaction model)
        {
            // TODO: Complete member initialization

        }
        public ObservableCollection ViewAKA
        {
            get { return _ViewAKA; }
            set { SetProperty(ref _ViewAKA, value); }
        }
    }

I bind with TestView.xaml file

<Window x:Class="EmployeeManager.View.TestView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestView" Height="359.774" Width="542.481">
    <Grid Margin="0,0,2,0">
        <Label Content="{Binding UpdatePercentage,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Background="Red" Foreground="White" Margin="130,86,0,0" VerticalAlignment="Top" Width="132" Height="39">

        </Label>

    </Grid>
</Window>

How can i realtime update Percentage in Label? quite newbie in WPF as well as MVVM

it is just test project with SQLite. Here is the link

https://onedrive.live.com/redir?resid=CBD5EFBEC55FE8C1!107&amp;authkey=!AJgQfP3vplSeHSs&amp;ithint=file,zip

THanks
Weeiyourda

Comments

  • What you can do is in the background thread, each time you need to update the percentage label, call it by invoking the application to update it. E.g.

    Application.Current.Dispatcher.Invoke(() => { 
        //update your percentage 
    });
    

    It would be a good idea to implement OnPropertyChanged as well, so you can notify when the property changes.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion