2

在我的 WPF 应用程序中,我有一个绑定到 ObservableCollection 的 DataGrid。

    <DataGrid x:Name="DataGridTeilnehmer" HorizontalAlignment="Left" VerticalAlignment="Top" CellEditEnding="DataGridTeilnehmer_CellEditEnding" AutoGenerateColumns="False" SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Teilnehmer" CellEditingTemplate="{StaticResource TeilnehmerEditTemplate}" CellTemplate="{StaticResource TeilnehmerCellTemplate}" />
            <DataGridComboBoxColumn Header="Pass" />
                    ...

DataGridComboBoxColumn 应为每一行填充单独的值。这些值取决于第一列的条目。所以,我想在 CellEditEnding 事件中设置数据,如下所示:

    private void DataGridTeilnehmer_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if (!commiting)
        {
          commiting = true;
            DataGridTeilnehmer.CommitEdit(DataGridEditingUnit.Row, false);
            commiting = false;

           // check, whether it is the first column that has been edited
           if (...)
             // get the list<string> for the combobox depending on the edited content
             // get the combobox of the current row and bind the calculated list<string> to it
        }
    }
}

我怎样才能做到这一点?

编辑:我想要实现的一个例子。

我有客户名单,每个客户都有单独的票。在第一列中选​​择了客户后,我想加载该客户拥有的票证列表并将其加载到下一列 - 组合框列。

提前致谢,
弗兰克

4

1 回答 1

0

如果您将数据网格绑定到 ObservableCollection 并且您的对象实现了 INotifyPropertyChanged,您可以在不使用单元格编辑结束事件的情况下实现您所需要的。

在您的模型中,只需检查第一列的值,然后设置其他列的值:

private string _firstColumn;
public string FirstColumn
{
    get { return _firstColumn; }
    set { 
         _firstColumn = value; 
         if(value == ...)
         //set other properties
         ...
         //notify the change
         OnPropertyChanged("FirstColumn"); }
}

当您的 datagridrow 失去焦点时,所有新值都会被通知给 datagrid

于 2012-11-22T14:58:17.467 回答