DataGrid.SelectedCellsChanged 活動
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當集合變更 SelectedCells 時會發生。
public:
event System::Windows::Controls::SelectedCellsChangedEventHandler ^ SelectedCellsChanged;
public event System.Windows.Controls.SelectedCellsChangedEventHandler SelectedCellsChanged;
member this.SelectedCellsChanged : System.Windows.Controls.SelectedCellsChangedEventHandler
Public Custom Event SelectedCellsChanged As SelectedCellsChangedEventHandler
Public Event SelectedCellsChanged As SelectedCellsChangedEventHandler
事件類型
範例
以下範例說明如何處理 SelectedCellsChanged 事件並清除新選取的儲存格中的值。
<Grid>
<DataGrid Name="DG1" ItemsSource="{Binding}" SelectionUnit="CellOrRowHeader" SelectionChanged="DG1_SelectionChanged" SelectedCellsChanged="DG1_SelectedCellsChanged" />
</Grid>
private void DG1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
//Get the newly selected cells
IList<DataGridCellInfo> selectedcells = e.AddedCells;
//Get the value of each newly selected cell
foreach (DataGridCellInfo di in selectedcells)
{
//Cast the DataGridCellInfo.Item to the source object type
//In this case the ItemsSource is a DataTable and individual items are DataRows
DataRowView dvr = (DataRowView)di.Item;
//Clear values for all newly selected cells
AdventureWorksLT2008DataSet.CustomerRow cr = (AdventureWorksLT2008DataSet.CustomerRow)dvr.Row;
cr.BeginEdit();
cr.SetField(di.Column.DisplayIndex, "");
cr.EndEdit();
}
}
Private Sub DG1_SelectedCellsChanged(sender As Object, e As SelectedCellsChangedEventArgs)
'Get the newly selected cells
Dim selectedcells As IList(Of DataGridCellInfo) = e.AddedCells
'Get the value of each newly selected cell
For Each di As DataGridCellInfo In selectedcells
'Cast the DataGridCellInfo.Item to the source object type
'In this case the ItemsSource is a DataTable and individual items are DataRows
Dim dvr As DataRowView = DirectCast(di.Item, DataRowView)
'Clear values for all newly selected cells
Dim cr As AdventureWorksLT2008DataSet.CustomerRow = DirectCast(dvr.Row, AdventureWorksLT2008DataSet.CustomerRow)
cr.BeginEdit()
cr.SetField(di.Column.DisplayIndex, "")
cr.EndEdit()
Next
End Sub
備註
你可以處理 SelectedCellsChanged 當所選儲存格集合被更改時會收到的通知事件。 若選出包含整排, Selector.SelectionChanged 事件也將提升。
你可以在事件處理器中從 中AddedCells取得 RemovedCells 和 SelectedCellsChangedEventArgs 。