How to bind a property of a child object inside a View in XAML

0
Hello, I'm using MVVM where I have an abstract class as default for my ViewModels:

public abstract class ViewModel : INotifyPropertyChanged
{
    public void Set<T>(ref T property, T value, [CallerMemberName]string propertyName = "")
    {
        property = value;
        NotifyPropertyChanged(propertyName);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

From here, I'll deploy the following VMs, such as AppViewModel:

public class AppViewModel : ViewModel
{
    public AppViewModel() 
    {
        var _fileVersion = Application.ResourceAssembly
            .GetCustomAttribute<AssemblyFileVersionAttribute>();

        FileVersion = _fileVersion != null ? _fileVersion.Version : "";

    }
    private string fileVersion;
    public string FileVersion
    {
        get => fileVersion;
        set => Set(ref fileVersion, value);
    }
}

In certain situations, it is possible for me to use more than 1 VM in the same View so I built a test view like this:

public partial class Teste : Window
{
    public Teste()
    {
        InitializeComponent();
        DataContext = this;
        AppVM = new AppViewModel();
    }
    private AppViewModel AppVM { get; set; }
}
<Window 
    x:Class="App.Teste"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid>
        <Label Content="{Binding AppVM.FileVersion}" />
    </Grid>
</Window>

When debugging, I realize that the AppVM property is initialized but my XAML is not updated.

What do I need to do to be able to bind sub-properties like this?

  

I know that if I put my View having DataContext = new AppViewModel() it works, but so I could not have more than 1 ViewModel for each View ...

    
asked by anonymous 04.12.2018 / 17:13

0 answers