あなたのために何かがうまくいかなかったのはわかりませんが、これは私のために働いています。 MyRowItem
は、INotifyPropertyChanged
を実装する単なるランダムなクラスです。
値を往復させるには配線がもう少し必要です。奇妙なことがあります。
private void DataGrid_SelectedCellsChanged(object sender,
SelectedCellsChangedEventArgs e)
{
// For multiselection, e.AddedCells is a collection of all
// currently selected cells as DataGridCellInfo
var currentCell = (sender as DataGrid).CurrentCell;
var row = currentCell.Item as MyRowItem;
// Note that we're using the column header as a magic string.
// We could use the Tag property to make this slightly less
// fragile.
switch (currentCell.Column.Header.ToString())
{
case "Combo One":
row.TextCol = row.ComboColOne;
break;
case "Combo Two":
row.TextCol = row.ComboColTwo;
break;
}
}
XAML
<DataGrid
ItemsSource="{Binding Path=ItemCollection}"
SelectionUnit="CellOrRowHeader"
AutoGenerateColumns="False"
SelectedCellsChanged="DataGrid_SelectedCellsChanged"
>
<DataGrid.Columns>
<DataGridTextColumn
Header="Text"
Binding="{Binding TextCol}"
Width="120"
/>
<DataGridComboBoxColumn
Header="Combo One"
ItemsSource="{Binding Source={x:Static local:MyRowItem.BValues}}"
SelectedItemBinding="{Binding ComboColOne}"
Width="120"
/>
<DataGridComboBoxColumn
Header="Combo Two"
ItemsSource="{Binding Source={x:Static local:MyRowItem.CValues}}"
SelectedItemBinding="{Binding ComboColTwo}"
Width="120"
/>
</DataGrid.Columns>
</DataGrid>
あなたはDataGridCell.GotFocusイベントを処理しようとしたことがありますか?セルをクリックするとフォーカスが得られます。 – bamanow
他のセルの値は、コンボボックスで選択した値とどのように異なっていますか?それは計算されたものなのでしょうか? –
@bamamanow:GotFocusイベントハンドラは、DataGrid.CurrentCellプロパティと同じ情報を持ちます。 Ed Plunkett:データグリッドには複数のコンボボックス列があります。テキストブロックセルは、コンボボックス列のセルのいずれかの値で更新できます。問題は、コンボボックス選択時の値が変更されたときではなく、コンボボックスセルが選択されたときにテキストブロックを更新することです。 – Number8