I'm creating an application for UWP using MVVM principles.
I have a Page which contains the standard toolbar, with the actual content inside the ScrollView below it being dynamic depending on where the user navigated from;
<ScrollViewer>
<StackPanel Orientation="Vertical">
<ContentControl HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Content="{x:Bind viewModel.Content}">
</ContentControl>
</StackPanel>
</ScrollViewer>
A simplified version of the content I bind to that section looks something like this:
public class Content : StackPanel
{
ContentViewModel ViewModel;
TextBlock txtInformation;
public Content()
{
Orientation = Orientation.Vertical;
ViewModel = new ContentViewModel();
txtInformation = new TextBlock();
txtInformation.SetBinding(TextBlock.TextProperty,
new Binding()
{
Mode = BindingMode.OneWay,
Source = ViewModel.Item,
Path = new
PropertyPath("Information"),
TargetNullValue = "No information found"
});
Children.Add(txtInformation);
}
}
And lastly, here is what the simplified version of the view model for the content looks like:
public class ContentViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
MyObject _item;
public ContentViewModel()
{
Item = new MyObject()
{
Information = "Information goes here"
};
}
public MyObject Item
{
get
{
return _item;
}
set
{
_item = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Item"));
}
}
}
Initially the binding seems to work, as when the page loads the TextBlock has "No information found" as its text value. But when the property changes there's no update to the UI.
This only seems to be a problem when setting a control's binding in C# with the SetBinding() method instead of in XAML with the {x:Bind} extension as my other pages which use XAML for their binding update correctly.
No errors are thrown.