v3 (DelegateCommand) Command binding implementation
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)
Here is my code-behind class and data-model class
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?
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)
Code: Select all
<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>
Code: Select all
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?
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: v3 (DelegateCommand) Command binding implementation
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.
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.
Re: v3 (DelegateCommand) Command binding implementation
Now I understand.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.
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: Ahrefs [Bot], saji8k and 17 guests