title: How to reference an UI element from ViewModel (WPF)?
body: <p>This might be simple for you guys but am just starting in WPF, and always my mind thinks in terms of Winforms, and am wrong all the time.</p>
  
  <p>Anyway here is my situation.  I have label in my view like below:</p>
  
  <pre><code>UserControl
  
   &lt;UserControl.Resources&gt;
  
      &lt;Converters:BooleanToVisibilityConverter x:Key="visibilityConverter"&gt;&lt;/Converters:BooleanToVisibilityConverter&gt;
  
      &lt;!-- Error Handling --&gt;
      &lt;Converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /&gt;
  
      &lt;Converters:ErrorConverter x:Key="errorConverter"/&gt;
      &lt;ControlTemplate x:Key="ErrorTemplate"&gt;
          &lt;Border BorderBrush="Red" BorderThickness="2"&gt;
              &lt;AdornedElementPlaceholder /&gt;
          &lt;/Border&gt;
      &lt;/ControlTemplate&gt;
      &lt;Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="Validation.HasError" Value="true"&gt;
                  &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource errorConverter}}"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="comboBoxInError" TargetType="{x:Type ComboBox}"&gt;
          &lt;Style.Triggers&gt;
              &lt;Trigger Property="Validation.HasError" Value="true"&gt;
                  &lt;Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors), Converter={StaticResource errorConverter}}"/&gt;
              &lt;/Trigger&gt;
          &lt;/Style.Triggers&gt;
      &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  </code></pre>
  
  <p>Label</p>
  
  <pre><code>&lt;Label Name="IsImageValid"  Content="Image Created" Margin="0,7,-1,0" Style="{StaticResource LabelField}"
                      Grid.ColumnSpan="2" Grid.Row="15" Width="90" Height="28" Grid.RowSpan="2"
                      Grid.Column="1" IsEnabled="True" 
                      Visibility="{Binding IsImageValid,Converter={StaticResource BooleanToVisibilityConverter}}" /&gt;
  </code></pre>
  
  <p>I am trying to call the this label in my view model but not sure how.</p>
  
  <p>I have t following method in the viewmodel, planning to use the label to display some message based on the condition like below.</p>
  
  <pre><code>  ViewModel
  
   public class MetadataViewModel : NotificationObject, IMetadataViewModel
  {
      #region :: Properties ::
  
      private IEventAggregator eventAggregator;
      private IImageResizerService imageResizer;
  
      private string headerInfo;
      public string HeaderInfo
      {
          get
          {
              return headerInfo;
          }
          set
          {
              if (this.headerInfo != value)
              {
                  this.headerInfo = value;
                  this.RaisePropertyChanged(() =&gt; this.HeaderInfo);
              }
          }
      }
  
      public ICommand SaveCommand
      {
          get;
          private set;
      }
  
      public ICommand CloseCommand
      {
          get;
          private set;
      }
  
      public ICommand DeleteCommand
      {
          get;
          private set;
      }
  
      public ICommand SubmitCommand
      {
          get;
          private set;
      }
  
      public ICommand UnSubmitCommand
      {
          get;
          private set;
      }
  
      public ICommand LocationSearchCommand
      {
          get;
          private set;
      }
  
      public ICommand SubjectSearchCommand
      {
          get;
          private set;
      }
  
      public ICommand RemoveLocationCommand
      {
          get;
          private set;
      }
  
      public ICommand RemoveSubjectCommand
      {
          get;
          private set;
      }
  
      private StoryItem selectedStory;
      public StoryItem SelectedStory
      {
          get
          {
              return this.selectedStory;
          }
          set
          {
              if (this.selectedStory != value)
              {
                  this.selectedStory = value;
                  this.RaisePropertyChanged(() =&gt; this.SelectedStory);
  
                  // raise dependencies
                  this.RaisePropertyChanged(() =&gt; this.CanSave);
                  this.RaisePropertyChanged(() =&gt; this.CanUnSubmit);
                  this.RaisePropertyChanged(() =&gt; this.CanDelete);
  
              }
          }
      }
  
  
      public List&lt;Program&gt; ProgramList 
      { 
          get; 
          private set; 
      }
  
      public List&lt;Genre&gt; GenreList 
      { 
          get; 
          private set; 
      }
  
      public List&lt;Copyright&gt; CopyrightList 
      { 
          get; 
          private set; 
      }
  
      public bool CanSave
      {
          get
          {
              bool canSave = false;
  
              if (this.SelectedStory.IsLockAvailable)
              {
                  if (!this.SelectedStory.Submitted)
                  {
                      canSave = true;
                  }
              }
  
              return canSave;
          }
      }
  
      public bool CanDelete
      {
          get
          {
              bool canDelete = false;
  
              if (this.SelectedStory.IsLockAvailable)
              {
                  if (!this.SelectedStory.Submitted)
                  {
                      canDelete = true;
                  }
              }
  
              return canDelete;
          }
      }
  
  
  
      public bool CanUnSubmit
      {
          get
          {
              bool canUnSubmit = false;
  
              if (this.SelectedStory.IsLockAvailable)
              {
                  if (this.SelectedStory.Submitted)
                  {
                      canUnSubmit = true;
                  }
              }
  
              return canUnSubmit;
          }
      }
  
      #endregion
  
      #region :: Contructor ::
  
      [ImportingConstructor]
      public MetadataViewModel(
              IMetadataController metadataController, 
              IGatewayService gateway, 
              INavigationService navigator,
              IImageResizerService imageResizer,
              IEventAggregator eventAggregator
          )
      {
          this.eventAggregator = eventAggregator;
          this.imageResizer = imageResizer;
  
          // populate drop-down lists
          this.ProgramList = gateway.GetPrograms(true);
          this.GenreList = gateway.GetGenres();
          this.CopyrightList = gateway.GetCopyrights();
  
          // add dummy values so the user can de-select
          this.ProgramList.Add(new Program());
          this.GenreList.Add(new Genre());
          this.CopyrightList.Add(new Copyright());
  
          // commands
          this.SaveCommand = metadataController.SaveCommand;
          this.CloseCommand = metadataController.CloseCommand;
          this.DeleteCommand = metadataController.DeleteCommand;
          this.SubmitCommand = metadataController.SubmitCommand;
          this.UnSubmitCommand = metadataController.UnSubmitCommand;
  
  
  
          this.LocationSearchCommand = new DelegateCommand&lt;string&gt;(this.LocationSearch);
          this.SubjectSearchCommand = new DelegateCommand&lt;string&gt;(this.SubjectSearch);
          this.RemoveLocationCommand = new DelegateCommand&lt;Topic&gt;(this.RemoveLocation);
          this.RemoveSubjectCommand = new DelegateCommand&lt;Topic&gt;(this.RemoveSubject);
  
          // events
          this.eventAggregator.GetEvent&lt;StorySelectedEvent&gt;().Subscribe(OnStorySelected, ThreadOption.UIThread);
          this.eventAggregator.GetEvent&lt;AddLocationEvent&gt;().Subscribe(OnAddLocation, ThreadOption.UIThread);
          this.eventAggregator.GetEvent&lt;AddSubjectEvent&gt;().Subscribe(OnAddSubject, ThreadOption.UIThread);
          this.eventAggregator.GetEvent&lt;CommandCompletedEvent&gt;().Subscribe(OnCommandCompleted, ThreadOption.UIThread);
          this.eventAggregator.GetEvent&lt;ImageResizeCompletedEvent&gt;().Subscribe(OnImageResizeCompleted, ThreadOption.UIThread);
  
          this.Initialize();
      }
  
      #endregion
  
      private void OnStorySelected(StoryItem selectedStory)
      {
          if (this.selectedStory != null)
          {
              this.Initialize();
  
              // override the initialized values
              this.SelectedStory = selectedStory;
              this.SelectedStory.HaveChanged = false;
              this.HeaderInfo = "Edit";
          }
      }
  
      public void OnAddLocation(Topic topic)
      {
          if (topic != null)
          {
              if (!this.SelectedStory.Locations.Contains(topic))
              {
                  this.SelectedStory.Locations.Add(topic);
                  this.RaisePropertyChanged(() =&gt; this.SelectedStory.Locations);
              }
          }
      }
  
      public void OnAddSubject(Topic topic)
      {
          if (topic != null)
          {
              if (!this.SelectedStory.Subjects.Contains(topic))
              {
                  this.SelectedStory.Subjects.Add(topic);
                  this.RaisePropertyChanged(() =&gt; this.SelectedStory.Subjects);
              }
          }
      }
  
      private void OnCommandCompleted(string commandType)
      {
          if (commandType == CommandTypes.MetadataEntry)
          {
              this.Initialize();
          }
      }
  
      private void OnImageResizeCompleted(bool isSuccessful)
      {
          IsImageValid = false;
          if (isSuccessful)
          {
  
              this.SelectedStory.KeyframeImages = true;
              IsImageValid = true;
          }
          else
          {
              this.SelectedStory.KeyframeImages = false;
              IsImageValid=false;
          }
      }
  
      private void Initialize()
      {
          this.SelectedStory = new StoryItem();
          this.HeaderInfo = "Create";
      }
  
      private void LocationSearch(object topicType)
      {
          this.eventAggregator.GetEvent&lt;LocationSearchEvent&gt;().Publish(null);
      }
  
      private void SubjectSearch(object topicType)
      {
          this.eventAggregator.GetEvent&lt;SubjectSearchEvent&gt;().Publish(null);
      }
  
      private void RemoveLocation(Topic selected)
      {
          if (selected != null)
          {
              // remove the primary too
              if (this.SelectedStory.PrimaryLocation != null)
              {
                  if (string.Equals(this.SelectedStory.PrimaryLocation.FullName, selected.FullName, StringComparison.InvariantCultureIgnoreCase))
                  {
                      this.SelectedStory.PrimaryLocation = new Topic();
                  }
              }
  
              bool isSuccessful = this.SelectedStory.Locations.Remove(selected);
              if (isSuccessful)
              {
                  this.RaisePropertyChanged(() =&gt; this.SelectedStory.Locations);
              }
          }
      }
  
      private void RemoveSubject(Topic selected)
      {
          if (selected != null)
          {
              // remove the primary too
              if (this.SelectedStory.PrimarySubject != null)
              {
                  if (string.Equals(this.SelectedStory.PrimarySubject.FullName, selected.FullName, StringComparison.InvariantCultureIgnoreCase))
                  {
                      this.SelectedStory.PrimarySubject = new Topic();
                  }
              }
  
              bool isSuccessful = this.SelectedStory.Subjects.Remove(selected);
              if (isSuccessful)
              {
                  this.RaisePropertyChanged(() =&gt; this.SelectedStory.Subjects);
              }
          }
      }
  }
  
          private booly _isImageValid;
  
          public bool IsImageValid
          {
          get
          { 
              return _isImageValid;
          }
          set
          {
              _isImageValid = value;
              this.RaisePropertyChanged(() =&gt; this.IsImageValid);
          }
      }
  }
  </code></pre>
  
  <p>Honestly i don't know how view will understand the binding.</p>