2012-10-10 102 views
3

我在DataGrid(WPF Toolkit)中绑定SelectedItem时遇到问题。当我从主窗体中打开UserControl时,SelectedItem不显示在DataGrid中。但是如果要在调试器中查看,那么一切都是正确的,并且selecteditem具有一定的价值。然后,例如,如果我再次设置值SelectedItem(在代码中),DataGrid开始正确显示SelectedItem。DataGrid中的SelectedItem MVVM Light

这里是我的代码的一部分:

DictionaryView.xaml

<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}"> 
<Grid> 
    <Label Content="{Binding Title}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="300" FontWeight="Bold" HorizontalContentAlignment="Center" FontSize="14" /> 
    <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"> 
     <my:DataGrid.Columns> 
      <my:DataGridTextColumn Header="ID" Width="50" Binding="{Binding ID}" IsReadOnly="True"/> 
      <my:DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" IsReadOnly="True" /> 
     </my:DataGrid.Columns> 

    </my:DataGrid> 
    <ToolBarTray Height="26" HorizontalAlignment="Left" Margin="0,27,0,0" Name="toolBarTray1" VerticalAlignment="Top" Width="300" IsLocked="True"> 
     <ToolBar> 
      <ToolBar.Items> 
       <Button Content="Add" Command="{Binding AddElementCommand}"/> 
       <Button Content="Edit" Command="{Binding EditElementCommand}"/> 
       <Button Content="Delete" Command="{Binding DeleteElementsCommand}"/> 
      </ToolBar.Items> 
     </ToolBar> 
    </ToolBarTray> 
</Grid> 

DictionaryViewModel

public DictionaryRecord SelectedDictionaryItem 
{ 
    get { return _selectedDictionaryItem; } 
    set 
    { 
     if (_selectedDictionaryItem == value) return; 
     _selectedDictionaryItem = value; 
     RaisePropertyChanged("SelectedDictionaryItem"); 
    } 

} 

public IEnumerable<DictionaryRecord> 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<ShowDictionaryMessage>(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; 
    } 
} 

它更是一个很少的代码和图片。

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(); 
     } 

在这里,我看到开幕形式

........

时,但SelectedDictionaryItem具有价值,如果我按“编辑”与我需要的数据形式正确地打开。

如果我执行SomeSimpleCommand。我看到下面的

........

我需要知道为什么在第一种情况下我没有看到的的SelectedItem深蓝色线。

P.S.对不起,我的英语不好。

+0

刚刚编辑我的回复现在我可以看到你在做什么 – SteveL

回答

0

DIT: 确定 - 可以看到你想现在该怎么办...

我已经在过去类似的问题,虽然这是与第三方控制。我想我所遇到的是在网格中加载数据之前选定的项目被设置。

我最终不得不做了一点小事,在网格的数据加载事件后面留下了一些代码。 VM是存储参照视图模型模块级变量 - 或者你可以得到由this.DataContext作为视图模型因为你似乎使用定位来得到你:

private void CaseGridView_DataLoaded(object sender, EventArgs e) 
    { 
     var grid = sender as GridView; 
     if (grid != null) 
     { 
      grid.SelectedItem = vm.CurrentlySelectedItem; 
     } 
    } 

的另一件事值得尝试的UpdateLayout请( ),以确保它刷新

+0

谢谢。它有帮助。 – Ged