Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF nested data binding to control - why does it not work

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));
    }
}
like image 682
Poku Avatar asked Apr 25 '26 12:04

Poku


1 Answers

Here's https://msdn.microsoft.com/en-us/library/ms752347%28v=vs.100%29.aspx

Look at the Providing Visual Feedback section, it's a Trigger binding property but they used a dotted property

Same for collection views section sub -> How to create a view

They used a dotted property on Application object.

Look at your output view when you are debugging your GUI, if WPF does not find a property in a Data context, it will throw a log !

like image 185
A.Caillet Avatar answered Apr 28 '26 00:04

A.Caillet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!