1

I have this App.xaml.cs code in my WPF project:

public partial class App : Application
{
    public static bool IsInitialized
    {
        get;
        private set;
    }

    public static async Task Initialize()
    {
        // Mark application as initialized
        IsInitialized = true;
    }
}

Main window of my application should be disabled (IsEnabled== False) while App.IsInitialized flag is not set, so window gets enabled when Initialize() finished.

How to achieve this?

Tried to use this XAML:

IsEnabled="{Binding App.IsInitialized, Mode=TwoWay}"
Croll
  • 3,631
  • 6
  • 30
  • 63
  • The binding has absolutely no idea what `App` is. You have to define an xml namespace (xmlns) that is for the namespace in which your App is defined, then you must prefix the App in the binding with this namespace so the Binding can identify where to locate App, and lastly you have to implment INotifyPropertyChanged so that the Binding will be able to see when you update IsInitialized. You've got a lot of stuff to learn. –  Jun 24 '15 at 16:36
  • @Will i only asking for example impementing INotifyPropertyChanged, here is nothing to learn, only example. – Croll Jun 24 '15 at 17:04

3 Answers3

3

You may use:

IsEnabled="{Binding Source={x:Static Application.Current}, Path=Initialized}"

And also you should notify when the property Initialized gets updated in order to get the UI updated as well, for this you should implemented the INotifyPropertyChanged interface and raise the PropertyChange event on your Initialize() method.

Hop this helps.

Arnel
  • 332
  • 2
  • 15
-1

Taken (and modified) from MS example in the documentation:

<Binding Source="{x:Static Application.Current}" Path="Initialized"/>
Alexander Balabin
  • 2,055
  • 11
  • 13
  • Well I guess I misunderstood your question. Regarding the real question I cannot see why the IsInitialized property has to be static - the App is a singleton anyway and you can access it via Current static property. Once you make IsInitialized an instance property implementing property change notifications becomes easy. – Alexander Balabin Jun 24 '15 at 17:18
-1

Yes, static is incorrect in most of cases, so i gonna implement INotifyPropertyChanged so UI will receive update notifications from my 'controller' class.

Also, this is brilliant code for future: https://gist.github.com/schuster-rainer/2644730

This is INotifyPropertyChanged implementation sample.

public class AppController : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private bool m_bInit;
        private PropertyChangedEventArgs m_bInitEA = new PropertyChangedEventArgs("IsInitialized");
        public bool IsInitialized
        {
            get { return m_bInit; }
            set
            {
                m_bInit = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, m_bInitEA);
            }
        }
    }

This is XAML:

<Window x:Class=".......
        Loaded="OnLoaded" DataContext="{x:Static Application.Current}"
        IsEnabled="{Binding Controller.IsInitialized}">
Croll
  • 3,631
  • 6
  • 30
  • 63
  • 1
    "brillant code for future" is mostly irrelevant to this question and answer. You are asking for vary basic bindings, `DynamicBindingProxy` is not needed. – quetzalcoatl Jun 24 '15 at 18:16
  • also, this implementation of INotifyPropertyChanged is flawed. You've got a classic and well known race condition between `if( propch != null)` and `propch(this,arg)` invocation, not mentioning that it doesn't even check if the `value` is really different from `m_bInit`. – quetzalcoatl Jun 24 '15 at 18:18
  • Thanks for hot advices – Croll Jun 24 '15 at 18:20
  • 1
    [see here for some good examples](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist), but take care as they mostly contain some extra flavors that you may don't need or feel comfortable with at first. – quetzalcoatl Jun 24 '15 at 18:21