title: SelectedItem in DataGrid MVVM Light
body: <p>I have problem with binding SelectedItem in DataGrid(WPF Toolkit). When I open UserControl from main form SelectedItem don't displayed in DataGrid. But if to look in a debugger, there everything is allright, and selecteditem has some value. Then for example if I again setting   value SelectedItem (in code), DataGrid starts to displayed SelectedItem correctly.</p>
  
  <p>Here is the part of my Code:</p>
  
  <p>DictionaryView.xaml</p>
  
  <pre><code>&lt;UserControl x:Class="AccountingCatridge.Views.DictionaryView"
           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" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300" 
           xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
           DataContext="{Binding Source={StaticResource Locator}, Path=Dictionary}"&gt;
  &lt;Grid&gt;
      &lt;Label Content="{Binding Title}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" FontWeight="Bold" HorizontalContentAlignment="Center" FontSize="14" /&gt;
      &lt;my:DataGrid x:Name="dataGrid"
                   Height="247" 
                   HorizontalAlignment="Left" 
                   Margin="0,53,0,0" 
                   VerticalAlignment="Top"
                   Width="300" 
                   IsReadOnly="True"
                   AutoGenerateColumns="False"
                   ItemsSource="{Binding DataItems}" 
                   SelectedItem="{Binding SelectedDictionaryItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                   HeadersVisibility="None"&gt;
          &lt;my:DataGrid.Columns&gt;
              &lt;my:DataGridTextColumn Header="ID" Width="50" Binding="{Binding ID}" IsReadOnly="True"/&gt;
              &lt;my:DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" IsReadOnly="True" /&gt;
          &lt;/my:DataGrid.Columns&gt;
  
      &lt;/my:DataGrid&gt;
      &lt;ToolBarTray Height="26" HorizontalAlignment="Left" Margin="0,27,0,0" Name="toolBarTray1" VerticalAlignment="Top" Width="300" IsLocked="True"&gt;
          &lt;ToolBar&gt;
              &lt;ToolBar.Items&gt;
                  &lt;Button Content="Add" Command="{Binding AddElementCommand}"/&gt;
                  &lt;Button Content="Edit" Command="{Binding EditElementCommand}"/&gt;
                  &lt;Button Content="Delete" Command="{Binding DeleteElementsCommand}"/&gt;
              &lt;/ToolBar.Items&gt;
          &lt;/ToolBar&gt;
      &lt;/ToolBarTray&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>DictionaryViewModel</p>
  
  <pre><code>public DictionaryRecord SelectedDictionaryItem
  {
      get { return _selectedDictionaryItem; }
      set
      {
          if (_selectedDictionaryItem == value) return;
          _selectedDictionaryItem = value;
          RaisePropertyChanged("SelectedDictionaryItem");
      }
  
  }
  
  public IEnumerable&lt;DictionaryRecord&gt; DataItems
  {
      get { return _dataItems; } 
      set
      {
          if (_dataItems == value) return;
          _dataItems = value;
          RaisePropertyChanged("DataItems");
          SelectedDictionaryItem = _dataItems.First();
      }
  }
  
  public DictionaryViewModel(IDataService dataService)
  {
      _dataService = dataService;
      Messenger.Default.Register&lt;ShowDictionaryMessage&gt;(this, ShowDictionary);
  }
  
  private void ShowDictionary(ShowDictionaryMessage mes)
  {
      _typeDict = mes.TypeDict;
      Title = StringEnum.GetStringValue(_typeDict);
  
      switch (_typeDict)
      {
          case TypeDictionary.Employees:
              DataItems = _dataService.GetEmployees();
              break;
          case TypeDictionary.ModelPrinters:
              DataItems = _dataService.GetPrinters();
              break;
      }
  }
  </code></pre>
  
  <p>it is a little more code and images. </p>
  
  <pre><code>    public RelayCommand EditElementCommand
          {
              get { return _editElementCommand ?? (_editElementCommand = new RelayCommand(EditElement)); }
          }
  
          private void EditElement()
          {
              if (SelectedDictionaryItem == null) return;
              Messenger.Default.Send(new ShowDictionaryRecordMessage{ Action = TypeRecordAction.Edit, Dictionary = _typeDict, Record = SelectedDictionaryItem});
          }
  
  public RelayCommand SomeSimpleCommand
          {
              get { return _someSimpleCommand ?? (_someSimpleCommand = new RelayCommand(SomeSimpleAction)); }
          }
  
          private void SomeSimpleAction()
          {
              SelectedDictionaryItem = _dataItems.Last();
          }
  </code></pre>
  
  <p>here that I see when opening form</p>
  
  <p><img src="https://i.stack.imgur.com/j6DTs.png" alt="........"></p>
  
  <p>but SelectedDictionaryItem has value and if I pressed "edit" the form with data necessary for me opens correctly.</p>
  
  <p>if I execute SomeSimpleCommand. I see the following</p>
  
  <p><img src="https://i.stack.imgur.com/LyKth.png" alt="........"> </p>
  
  <p>I need to know why in the first case I don't see a dark blue line for the SelectedItem.</p>
  
  <p>P.S. Sorry for my bad English.</p>