title: WPF MultiBinding Fails. Why?
body: <p>I have this Markup:</p>
  
  <pre><code>   &lt;GroupBox BorderThickness="2"&gt;
      &lt;GroupBox.BorderBrush&gt;
          &lt;SolidColorBrush x:Name="Border"&gt;
              &lt;SolidColorBrush.Color&gt;
                  &lt;MultiBinding Converter="{StaticResource ConnectionAndLoggedInToBorderBrush}"&gt;
                      &lt;Binding Path="IsConnected"/&gt;
                      &lt;Binding Path="IsLoggedIn"/&gt;
                  &lt;/MultiBinding&gt;
              &lt;/SolidColorBrush.Color&gt;
          &lt;/SolidColorBrush&gt;
      &lt;/GroupBox.BorderBrush&gt;
  </code></pre>
  
  <p>In the code behind I have this line in the window_loaded method:</p>
  
  <pre><code>DataContext = uiManager;
  </code></pre>
  
  <p>uiManager is of type UIManager that has two public properties named IsConnected and IsLoggedIn.</p>
  
  <p>This code fails at at startup because the values array in the Converter that is called by the Multibinding is not filled with booleans but have a value of DependencyProperty.UnsetValue.</p>
  
  <p>When I use the markup below (and change the returntype of the converter) it does work.</p>
  
  <pre><code>   &lt;GroupBox BorderThickness="2"&gt;
      &lt;GroupBox.BorderBrush&gt;
           &lt;MultiBinding Converter="{StaticResource ConnectionAndLoggedInToBorderBrush}"&gt;
                &lt;Binding Path="IsConnected"/&gt;
                &lt;Binding Path="IsLoggedIn"/&gt;
           &lt;/MultiBinding&gt;
      &lt;/GroupBox.BorderBrush&gt;
  </code></pre>
  
  <p>It seems that the binding set through the DataContext in the code behind fails in the first example, but works in the second one. Why?</p>
  
  <p>For completeness below the UIManager class:</p>
  
  <pre><code>public class UIManager:IUIManager
      {
  
          #region Implementation of IUIManager
  
          private const string IsLoggedInProperty = "IsLoggedIn";
          private bool loggedIn;
          private readonly object loggedInLock = new object();
          public bool IsLoggedIn
          {
              get
              {
                  lock (loggedInLock)
                  {
                      return loggedIn;
                  }
              }
              set
              {
                  lock (loggedInLock)
                  {
                      if(value==loggedIn)return;
                      loggedIn = value;
                      OnPropertyChanged(IsLoggedInProperty);
                  }
              }
          }
  
          private void OnPropertyChanged(string property)
          {
              if(PropertyChanged!=null)PropertyChanged(this,new PropertyChangedEventArgs(property));
          }
  
          private const string IsConnectedProperty = "IsConnected";
          private bool isConnected;
          private object isConnectedLock = new object();
          public bool IsConnected
          {
              get
              {
                  lock (isConnectedLock)
                  {
                      return isConnected;
                  }
              }
              set
              {
                  lock (isConnectedLock)
                  {
                      if(value==isConnected)return;
                      isConnected = value;
                      OnPropertyChanged(IsConnectedProperty);
                  }
              }
          }
  
          #endregion
  
          #region Implementation of INotifyPropertyChanged
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          #endregion
      }
  </code></pre>
  
  <p>EDIT:
  The conversion method for the failing XAML (it fails on the conversion to bool of values[0]:</p>
  
  <pre><code>public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
          {
              var is_connected = (bool) values[0];
              var is_loggedin = (bool) values[1];
              return is_loggedin
                         ? is_connected
                               ? Colors.YellowGreen
                               : Colors.Red
                         : Colors.Gray;
          }
  </code></pre>
  
  <p>for the working XAML:</p>
  
  <pre><code>public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
          {
              var is_connected = (bool) values[0];
              var is_loggedin = (bool) values[1];
              return is_loggedin
                         ? is_connected
                               ? Brushes.YellowGreen
                               : Brushes.Red
                         : Brushes.Gray;
          }
  </code></pre>