Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a problem with binding values to controls. I checked in other windows I have and I think I'm doing it the same way.
I want to show something like loading window from App before main window will open.

InitizalizationWindow:
C#
public InitializationWindow()
{
            ...
            InitializationWindowClass.Progress = new InitializationWindowClass();
            this.mainSP.DataContext = InitializationWindowClass.Progress;
}


and part of xaml:
C#
<StackPanel Name="mainSP" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Margin="0, 0, 0, 10">
        <TextBlock x:Name="tblProgress" FontSize="14" Text="{Binding ProgressText}" TextAlignment="Center" TextWrapping="Wrap" />
        <Grid>
                <telerik:RadProgressBar x:Name="progress" Value="{Binding ProgressValue}" telerik:StyleManager.Theme="Summer" Height="25" IsIndeterminate="False" />
                <Label x:Name="lblPercent" FontWeight="Bold" Content="{Binding ProgressValueString}" HorizontalAlignment="Center" VerticalContentAlignment="Center" />
        </Grid>
</StackPanel>


InitializationWindowClass:
C#
public class InitializationWindowClass : INotifyPropertyChanged
    {
        public static InitializationWindowClass Progress { get; set; }
        private string progressText = String.Empty, progressValueString = String.Empty;
        private int progressValue = 0;
        public event PropertyChangedEventHandler PropertyChanged;

        public string ProgressText
        {
            get
            {
                return progressText;
            }
            set
            {
                progressText = value;
                NotifyPropertyChanged("ProgressText");
            }
        }

        public string ProgressValueString
        {
            get
            {
                return progressValueString;
            }
            set
            {
                progressValueString = value;
                NotifyPropertyChanged("ProgressValueString");
            }
        }

        public int ProgressValue
        {
            get
            {
                return progressValue;
            }
            set
            {
                progressValue = value;
                ProgressValueString = String.Format("{0}%", progressValue);
                NotifyPropertyChanged("ProgressValue");
                NotifyPropertyChanged("ProgressValueString");
            }
        }

        private void NotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }


and part of App.xaml.cs:
C#
protected override void OnStartup(StartupEventArgs e)
{
    InitializationWindow iw = new InitializationWindow(); 
    iw.Show();
    InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)count / (decimal)sum) * 100);
    InitializationWindowClass.Progress.ProgressText = "Some text";
    ...
    ...
    InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)count / (decimal)sum) * 100);
    InitializationWindowClass.Progress.ProgressText = "New text";
    ...
    ...
}



I checked and when I'm changing i.e. ProgressValue from App.xaml.cs, the value is changing - going to
C#
get
{
    return progressValue;
}



So the question is - what I'm doing wrong?
Posted
Updated 15-Jan-15 3:07am
v3
Comments
Leo Chapiro 15-Jan-15 8:16am    
And what is the problem / error?
Cudak 15-Jan-15 8:23am    
nothing is changing (text/content/value of control)
TheRealSteveJudge 15-Jan-15 9:10am    
I assume your application is blocked until the OnStartup has finished.
When you have only one assignment of ProgressValue resp. ProgressText
the value is shown.

1 solution

Please try the app.xaml.cs below.

ProgressBar is growing and value is shown.

So your OnStartup method blocked the UI thread.

It is not a data binding problem.
C#
public partial class App : Application
  {
      public App()
      {
          this.Startup += App_Startup;
      }

      void App_Startup(object sender, StartupEventArgs e)
      {
          InitializationWindow iw = new InitializationWindow();
          iw.Show();

          Task task = new Task(Test);
          task.Start();
      }

      void Test()
      {
          for (int i = 0; i <= 100; i++)
          {
              InitializationWindowClass.Progress.ProgressValue = Convert.ToInt32(((decimal)i / (decimal)100) * 100);
              InitializationWindowClass.Progress.ProgressText = "Some text";

              Thread.Sleep(100);
          }
      }
}
 
Share this answer
 
v3
Comments
Cudak 16-Jan-15 5:33am    
Thank you, you were very close to my specify problem :) Finally I finished with something like this:
http://blog.dontpaniclabs.com/post/2013/11/14/Dynamic-Splash-Screens-in-WPF
TheRealSteveJudge 16-Jan-15 5:38am    
You're welcome!
If my solution helped you,
please mark it as solution.
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900