2011-09-11 116 views
0

*解決* 我終於解開了它的代碼「髒」的背後,可以一直清潔&更好MVVM雖然 觀察集合的東西仍然在做視圖模型...自動滾動到新添加的行中的datagridview MVVM光

public ViewModelLocator VML = new ViewModel.ViewModelLocator(); 

    private void btnAdd_Click(object sender, RoutedEventArgs e) 
    { 

     VML.MainMatch.Add(); 
      dataGrid.SelectedIndex = VML.MainMatch.Matches.Count - 1; 
      dataGrid.ScrollIntoView(VML.MainMatch.Matches, dataGrid.Columns[0]); 
    } 

我有一個DataGrid,並迷上了一個relaycommand一個按鈕添加到觀察集合記錄(DataGrid的的ItemSource)一認爲,以下工作正常:

檢視:

<Button x:Name="btnAdd" Content="{Binding Resource.btnAdd, Source={StaticResource CustomLocStrings}}" Width="100" Command="{Binding AddCommand, Mode=OneWay}" Margin="5" HorizontalAlignment="Center" IsEnabled="{Binding IsLimitedUser, Converter={StaticResource TrueToFalseConverter}}"/> 

視圖模型:

AddCommand = new RelayCommand(() => Add()); 

    private void Add() 
    { 
     game _game = new game(); 
     _game.recid = 1; 
     _game.teamid = GlobalVariables.CurrentUser.teamid; 

     if (this.games != null) 
     { 
      this.games.Add(new gameViewModel(_game)); 
     } 
     else 
     { 
      var _games = new List<gameViewModel>(); 
      _games.Add(new gameViewModel(_game)); 
      this.games = new ObservableCollection<gameViewModel>(_gamees); 
     } 

的問題是,該行是在數據網格的底部加入,外面的用戶查看。 他必須向下滾動以查看新記錄。 我想要的是,網格會自動對新記錄進行滾動查看。

我發現一個類似的問題在這裏(http://forums.lhotka.net/forums/p/9086/43212.aspx),但未能將其翻譯爲我的處境:

我試了一下與EventTocCommand

 <i:Interaction.Triggers> 
    <i:EventTrigger EventName="Click"> 
     <cmd:EventToCommand Command="{Binding Path=AddCommand}" PassEventArgsToCommand="True" CommandParameter="{Binding ElementName=dataGrid}"/> 
        </i:EventTrigger> 

然後

RelayCommand<EventArgs> AddCommand = new RelayCommand<EventArgs>(Add); 

但是當我點擊,我懷疑沒有任何反應我commandparameter的錯誤..

我希望有人有一個解決方案,因爲我的用戶界面是不是真的「用戶友好」的它的方式..提前

感謝,

邁克

回答

0

我還沒有試過,但我認爲設置SelectedItem將是值得一試。

的SelectedItem = 「{結合的SelectedItem}」

因此,創建你的對象,將其添加到網格,並設置的SelectedItem相同的(新的)對象。

+0

thnx但它似乎沒有反應,當我設置selectedgame ..也許我會在視圖的代碼後面做.. –