riveranb
Topic Author
Posts: 10
Joined: 20 Aug 2019, 05:48

v3 (DelegateCommand) Command binding implementation

22 Jul 2020, 16:07

I am trying to upgrade NoesisGUI from v2.2.x to v3.0.x for my project.
After several days survey and trying.
I found that DelegateCommand instance setup for command binding have something different.

I created a test user-control (Test3Window)
    <Grid>
        <StackPanel>
            <Button Content="Command-1" 
                    Command="{Binding Command1}"
                    Height="40" Margin="40"/>
            <Button Content="Command-2"
                    Command="{Binding Command2}"
                    Height="40" Margin="40"/>
        </StackPanel>    
    </Grid>
Here is my code-behind class and data-model class
    public class Test3Model
    {
        public DelegateCommand Command1 { get; set; }
        public DelegateCommand Command2 { get; set; }
    }

    public partial class Test3Window : UserControl
    {
        public Test3Window()
        {
            Initialized += OnInitialized;
            InitializeComponent();
        }

        #region _Methods
        private void InitializeComponent()
        {
            Noesis.GUI.LoadComponent(this, "Assets/Xamls/Test3Window.xaml");
        }

        private void OnInitialized(object sender, EventArgs args)
        {
            var model = new Test3Model();

#if NOESISGUI
            model.Command1 = new DelegateCommand(p =>
            {
                Debug.Log("Command1");
            });
            model.Command2 = new DelegateCommand(p =>
            {
                Debug.Log("Command2");
            });
#endif

            DataContext = model;
        }
        #endregion

    }

If I move "#if NOESISGUI #endif" block, codes of setup DelegateCommand after assign "DataContext = model",
then commands did not work in v3, but it worked in v2.

I wanna know if this is necessary to setup DelegateCommand instances BEFORE assign DataContext for proper command-binding in XAML files?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1974
Joined: 22 Dec 2011, 19:20

Re: v3 (DelegateCommand) Command binding implementation

22 Jul 2020, 16:14

If your ViewModel does not implement INotifyPropertyChanged and notifies with PropertyChanged event when a property changes, then you should set the properties before DataContext is set, otherwise property changes are not notified to the binding engine.

In 2.2 initialization process was a bit different, but in 3.0 we tried to follow WPF as much as possible. The behavior is correct now.
 
riveranb
Topic Author
Posts: 10
Joined: 20 Aug 2019, 05:48

Re: v3 (DelegateCommand) Command binding implementation

23 Jul 2020, 01:56

If your ViewModel does not implement INotifyPropertyChanged and notifies with PropertyChanged event when a property changes, then you should set the properties before DataContext is set, otherwise property changes are not notified to the binding engine.

In 2.2 initialization process was a bit different, but in 3.0 we tried to follow WPF as much as possible. The behavior is correct now.
Now I understand.
In official manual, example codes, never mentioned about PropertyChanged event.
So I almost forget this is also part of data-binding.
I agree that v3.0 behaviour is more WPF-like.

Who is online

Users browsing this forum: No registered users and 9 guests