title: Usercontrol not updating certain properties
body: <p>I have a class called MyItem that has an ID property, as well as three random number properties. I have a UserControl called MyItemControl that is designed to show a MyItem's ID, three random numbers, as well as the summation of those three random numbers. I display a collection of MyItems in a ListBox, and I have a contentcontrol that displays more detailed information about the ListBox's selected MyItem via the MyItemControl UserControl.</p>
  
  <p>However, I run into a problem where, when a MyItem is selected, the contentcontrol only updates the ID and random numbers, but not the summation. My guess is because the Sum property is calculated before a MyItem is bound to the MyItemControl. I've tried raising property changed when the MyItem property is set, but that doesn't seem to work either.</p>
  
  <p>How can I have the the Sum property update after the MyItem property is bound in the xaml?</p>
  
  <p>MyItem Class:</p>
  
  <pre><code>public class MyItem
  {
      private static Random random = new Random();
  
      public MyItem(string ID)
      {
          this.ID = ID;
          Num1 = random.Next();
          Num2 = random.Next();
          Num3 = random.Next();
      }
  
      public string ID { get; private set; }
  
      public int Num1 { get; private set; }
  
      public int Num2 { get; private set; }
  
      public int Num3 { get; private set; }
  
      public override string ToString()
      {
          return ID;
      }
  }
  </code></pre>
  
  <p>MyItemControl xaml:</p>
  
  <pre><code>&lt;UserControl x:Name="userControl" x:Class="testApp2.MyItemControl"
               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:local="clr-namespace:testApp2"
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      &lt;StackPanel Margin="0"&gt;
          &lt;StackPanel Margin="0" Orientation="Horizontal"&gt;
              &lt;Label Content="ID:" Margin="0" HorizontalAlignment="Left"/&gt;
              &lt;Label Content="{Binding MyItem.ID, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Margin="0" Orientation="Horizontal"&gt;
              &lt;Label Content="Num1:" Margin="0" HorizontalAlignment="Left"/&gt;
              &lt;Label Content="{Binding MyItem.Num1, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Margin="0" Orientation="Horizontal"&gt;
              &lt;Label Content="Num2:" Margin="0" HorizontalAlignment="Left"/&gt;
              &lt;Label Content="{Binding MyItem.Num2, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Margin="0" Orientation="Horizontal"&gt;
              &lt;Label Content="Num3:" Margin="0" HorizontalAlignment="Left"/&gt;
              &lt;Label Content="{Binding MyItem.Num3, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/&gt;
          &lt;/StackPanel&gt;
          &lt;StackPanel Margin="0" Orientation="Horizontal"&gt;
              &lt;Label Content="Sum:" Margin="0" HorizontalAlignment="Left"/&gt;
              &lt;Label Content="{Binding Sum, ElementName=userControl, Mode=OneWay}" Margin="0" HorizontalAlignment="Left"/&gt;
          &lt;/StackPanel&gt;
      &lt;/StackPanel&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>MyItemControl Code-behind:</p>
  
  <pre><code>public partial class MyItemControl : UserControl, INotifyPropertyChanged
  {
      public MyItemControl()
      {
          InitializeComponent();
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      protected void RaisePropertyChanged(string property)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
      }
  
      public MyItem MyItem
      {
          get { return (MyItem)GetValue(MyItemProperty); }
          set
          {
              SetValue(MyItemProperty, value);
              RaisePropertyChanged("MyItem");
              RaisePropertyChanged("Sum");
          }
      }
  
      public static readonly DependencyProperty MyItemProperty =
          DependencyProperty.Register("MyItem", typeof(MyItem), typeof(MyItemControl), new PropertyMetadata(null));
  
  
      public int Sum =&gt; MyItem != null ? MyItem.Num1 + MyItem.Num2 + MyItem.Num3 : 0;
  }
  </code></pre>
  
  <p>MainWindow xaml:</p>
  
  <pre><code>&lt;Window x:Name="window" x:Class="testApp2.MainWindow"
          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"
          xmlns:local="clr-namespace:testApp2"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Grid Margin="0"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition/&gt;
              &lt;ColumnDefinition/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;ListBox Margin="5" SelectionChanged="ItemSelectionChanged" ItemsSource="{Binding MyItems, ElementName=window}"/&gt;
          &lt;ContentControl Content="{Binding Selection, ElementName=window, Mode=OneWay}" Grid.Column="1" Margin="5"&gt;
              &lt;ContentControl.ContentTemplate&gt;
                  &lt;DataTemplate DataType="{x:Type local:MyItem}"&gt;
                      &lt;local:MyItemControl MyItem="{Binding}"/&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ContentControl.ContentTemplate&gt;
          &lt;/ContentControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>MainWindow Code-behind:</p>
  
  <pre><code>public partial class MainWindow : Window, INotifyPropertyChanged
  {
      public MainWindow()
      {
          MyItems = new ObservableCollection&lt;MyItem&gt;
          {
              new MyItem("Boop"),
              new MyItem("Bop"),
              new MyItem("Beep")
          };
          InitializeComponent();
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      protected void RaisePropertyChanged(string property)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
      }
  
      public ObservableCollection&lt;MyItem&gt; MyItems { get; private set; }
  
      private MyItem selection = null;
      public MyItem Selection
      {
          get { return selection; }
          set
          {
              selection = value;
              RaisePropertyChanged("Selection");
          }
      }
  
      private void ItemSelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          ListBox listbox = (ListBox)sender;
          if (listbox.SelectedIndex &gt; -1)
          {
              Selection = (MyItem)listbox.SelectedItem;
          }
      }
  }
  </code></pre>