2012-09-02 108 views
-1

後選擇新行我有在一個DataTable,它綁定到一個WPF DataGrid中一些數據的簡單的C#程序。添加一個新行到DataTable綁定到DataGrid

<DataGrid Grid.Row="1" DataContext="{Binding}" Name="dataGridEditTab" 

dataGridEditTab.ItemsSource = NavTable.DefaultView; 

我有一個「添加行」按鈕按下時,增加了一個空白行的數據表。空行顯示在DataGrid上,但未選中。我想自動選擇新的行,但我無法弄清楚如何做到這一點。我認爲我需要將SelectedItem設置爲某種東西,但我還沒有弄清楚是什麼。任何幫助?謝謝。

+0

DataGridCell已IsSelected proprty。使用它的行選擇 –

+0

你的問題的任何運氣? – Artiom

回答

0

在情況下,它是MVVM使用的SelectedItem或的SelectedIndex綁定。如果沒有,bettter使用它)

爲了達到你的目的只是綁定的SelectedIndex,只要你加入該行,將selectedIndex設置爲RowsCount-1;

這裏是一個例子。

<Grid > 

    <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" ItemsSource="{Binding Path=Persons}" Margin="0,65,0,0" 
        SelectedIndex="{Binding Path=SelectedIndex, Mode=TwoWay}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="First name" Binding="{Binding Path=FirstName}" Width="*"/> 
      <DataGridTextColumn Header="Last name" Binding="{Binding Path=LastName}" Width="*"/> 
     </DataGrid.Columns> 
    </DataGrid> 
    <Button Content="Add new row" Height="23" HorizontalAlignment="Left" Margin="317,12,0,0" Command="{Binding Path=AddCommand}" VerticalAlignment="Top" Width="112" /> 
</Grid> 

和視圖模型

public class MainViewModel : INotifyPropertyChanged 
{ 
    private int _selectedPerson; 

    public MainViewModel() 
    { 
     AddCommand = new RelayCommand(AddAndSelectePerson); 
     Persons = new DataTable(); 
     Persons.Columns.Add("FirstName"); 
     Persons.Columns.Add("LastName"); 
     Persons.Rows.Add("Alexandr", "Puskin"); 
     Persons.Rows.Add("Lev", "Tolstoy"); 
    } 

    public ICommand AddCommand { get; private set; } 

    public int SelectedIndex 
    { 
     get { return _selectedPerson; } 
     set 
     { 
      _selectedPerson = value; 
      OnPropertyChanged("SelectedIndex"); 
     } 
    } 

    public DataTable Persons { get; private set; } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    private void AddAndSelectePerson() 
    { 
     Persons.Rows.Add(); 
     SelectedIndex = Persons.Rows.Count - 1; 
    } 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public class RelayCommand : ICommand 
{ 
    private readonly Action _actionToExecute; 

    public RelayCommand(Action actionToExecute) 
    { 
     _actionToExecute = actionToExecute; 
    } 

    #region ICommand Members 

    public void Execute(object parameter) 
    { 
     _actionToExecute(); 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    #endregion 
} 

public class Person 
{ 
    public Person() 
    { 
    } 

    public Person(string firstName, string lastName) 
    { 
     FirstName = firstName; 
     LastName = lastName; 
    } 

    public string FirstName { get; set; } 

    public string LastName { get; set; } 
} 

Example嘗試