I have searched google and stackoverflow alot to find a answer to my question without any luck. I find the solution to the problem. Ex:
Data Binding to Nested Properties?
But i already know the solution. I want to know WHY wpf doesn't support nested/dotted data bindings on controls.
The solution is to set the DataContext of the parent control to the parent data object in my case my ViewModel property on my controller/window datacontext. So i could set the DataContext of my grid and my code would work if i change my TextBox binding to only use the Name property.
Another solution is to explicity set the UpdateSourceTrigger on my TextBox and keep my nested data binding on the TextBox control like below.
But why? Why doesn't WPF support nested binding like i do below without setting the UpdateSourceTrigger explicit? I would like to know that :).
I have this textbox control:
<Window>
<Grid>
<StackPanel>
<Label Content="Name" FontWeight="Bold"/>
<TextBox x:Name="NameTextBox" Text="{Binding Path=CreateEditAssetViewModel.Name, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Width="475" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalContentAlignment="Stretch" Margin="0, 5" />
</StackPanel>
</Grid>
</Window>
My window datacontext is bound like this:
var createEditWindow = new CreateEditWindow();
var createEditController = new CreateEditWindowController();
createEditWindow.DataContext = createEditController;
createEditWindow.Show();
My controller looks like this:
public class CreateEditWindowController : ViewModelBase, ICreateEditWindowController
{
private ICreateEditWindowViewModel _createEditWindowViewModel;
public ICreateEditWindowViewModel CreateEditAssetViewModel
{
get { return _createEditWindowViewModel; }
set
{
if (_createEditWindowViewModel == value) return;
_createEditWindowViewModel = value;
OnPropertyChanged(nameof(CreateEditAssetViewModel));
}
}
}
My ViewModel with the Name property that the textbox control binds to looks like this:
public class CreateEditWindowViewModel : ViewModelBase, ICreateEditWindowViewModel
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged(nameof(Name));
}
}
}
And my ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}