Using WPF and Prism.
I have a view, containing 1 textblock
<UserControl x:Class="ArmoryModule.Views.ResultsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Views="clr-namespace:ArmoryModule.Views" xmlns:vmdc="clr-namespace:ArmoryModule.ViewModels"
mc:Ignorable="d">
<UserControl.DataContext>
<vmdc:ResultsViewModel />
</UserControl.DataContext>
<Grid>
<TextBlock Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</UserControl>
attempting to bind to the pertinent part of ResultsViewModel below
class ResultsViewModel : ViewModel
{
private Character _character;
public Character Character
{
get { return _character; }
set
{
if (_character != value)
{
_character = value;
RaisePropertyChanged(() => Name);
}
}
}
public string Name
{
get { return _character.Name; }
}
}
Builds fine, breaks at startup. I get an object ref not sent to an instance of an object error on
get { return _character.Name; }
I know character isn’t populated at startup and thats the reason I’m getting the above error. I am kind of clueless as where to create Character so that the 2 viewmodels I have can use it.
Either add it to the constructor for the view model
or simply add a check in the getter, e.g.