-1

WPF DataGrid にバインドされている DataTable にいくつかのデータを含む単純な C# プログラムがあります。

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

dataGridEditTab.ItemsSource = NavTable.DefaultView;

押されたときに DataTable に空白行を追加する「行の追加」ボタンがあります。空白の行が DataGrid 罰金に表示されますが、選択されていません。新しい行を自動的に選択したいのですが、これを行う方法がわかりません。SelectedItem を何かに設定する必要があると思いますが、何がわかりません。何か助けはありますか?ありがとう。

4

2 に答える 2

0

MVVM の場合は、SelectedItem または SelectedIndex のバインドを使用します。そうでない場合は、それを使用することをお勧めします)

目標を達成するには、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; }
}

試す

于 2012-09-04T05:03:41.227 に答える
0

このスレッド (グリッドが並べ替えられた DataView にバインドされているときに DataGridView の選択した行を新しく追加された行に設定する方法) によると、 DataGridViewRowsAddedEventArgsを使用してこれを実現できます。基本的に、新しく追加された行インデックスを配置するグローバル変数を作成します。EventArgs でそのインデックスに適切な値を割り当て、そのインデックスを使用して探している行を選択します。

于 2012-09-02T20:40:47.400 に答える