0

I have a very simple compound property where I join 2 strings together to make a readonly third property so I can bind just 1 property to xaml.

public string LocationCompleteString => $"{LocationName}/{SubLocationName}";

Which works as expected, but issue is I also want to RaiseNotification for LocationCompleteString whenever "LocationName" or "SubLocationName" updates, for that I tried following:

public MyClass() => PropertyChanged += MyClass_PropertyChanged;
private void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if(e.PropertyName == nameof(LocationName) || e.PropertyName == nameof(SubLocationName))
        {
            RaisePropertyChanged(LocationCompleteString);
        }
    }

But this doesnt work because this doesnt update the value of LocationCompleteString so before raising notification I first need to somehow update its value, I am aware that I can make a private method which returns value for this property and then just before Raising notification for it I can update its value again with that method, but I was wondering if there was a better and more generic way to achieve it? maybe somehow I can call the "LocationCompleteString.Get()" method which will execute its get method again to update the value or something like that?

Sample project : https://github.com/touseefbsb/CompoundNotify

Thanks.

Muhammad Touseef
  • 4,357
  • 4
  • 31
  • 75

1 Answers1

1

It looks you have not call RaisePropertyChanged correctly, please refer to the following code. I have tested, it works well in my side.

public class MyClass : INotifyPropertyChanged
{
    public MyClass() => PropertyChanged += MyClass_PropertyChanged;

    public event PropertyChangedEventHandler PropertyChanged;

    private string _locationName;
    private string _subLocationName;
    private string _locationCompleteString;
    public string LocationName
    {
        get
        {
            return _locationName;
        }

        set
        {
            _locationName = value;
            RaisePropertyChanged( nameof(LocationName));
        }
    }
    public string SubLocationName
    {
        get
        {        
            return _subLocationName;
        }
        set
        {
            _subLocationName = value;
            RaisePropertyChanged(nameof(SubLocationName));
        }
    }
    public string LocationCompleteString => $"{LocationName}/{SubLocationName}";

    private void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(LocationName) || e.PropertyName == nameof(SubLocationName))
        {
            RaisePropertyChanged(nameof(LocationCompleteString));
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml

<StackPanel>
    <StackPanel.DataContext>
        <local:MyClass />
    </StackPanel.DataContext>
    <TextBlock x:Name="FullContent" Text="{Binding LocationCompleteString}" />
    <TextBox x:Name="Header" Text="{Binding LocationName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    <TextBox x:Name="Footer" Text="{Binding SubLocationName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • I have updated my sample project with the changes u showed in ur code, but it is still not working, I type text into those 2 textboxes on xaml page but they dnt update the "LocationCompleteString" textblock just below them. – Muhammad Touseef Sep 09 '21 at 08:39
  • Please go to this [line](https://github.com/touseefbsb/CompoundNotify/blob/master/App1/App1/ViewModels/MainViewModel.cs#L17), replace it with RaisePropertyChanged(nameof(LocationCompleteString)); – Nico Zhu Sep 09 '21 at 08:41
  • 1
    yeah u r right, that fixed it, Thanks a lot. – Muhammad Touseef Sep 09 '21 at 08:41