Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF:选择其他单元格时更新datagrid中的单元格_Wpf_Datagrid - Fatal编程技术网

WPF:选择其他单元格时更新datagrid中的单元格

WPF:选择其他单元格时更新datagrid中的单元格,wpf,datagrid,Wpf,Datagrid,我们有一个由DataGridTemplateColumns组成的DataGrid。 一些列包含简单的文本块,而另一些列使用组合框 当用户单击带有组合框的单元格时,我们需要使用单击组合框的当前值更新同一行中的文本块。 当combobox的选定值更改时,这很容易做到(当combobox的值更改时,绑定到combobox的属性会更新绑定到texblock的属性),但当combobox单元格仅被选中时,我不知道如何做到这一点。 数据网格上的SelectionUnit是CellOrRowHeader 我一

我们有一个由DataGridTemplateColumns组成的DataGrid。
一些列包含简单的文本块,而另一些列使用组合框

当用户单击带有组合框的单元格时,我们需要使用单击组合框的当前值更新同一行中的文本块。
当combobox的选定值更改时,这很容易做到(当combobox的值更改时,绑定到combobox的属性会更新绑定到texblock的属性),但当combobox单元格仅被选中时,我不知道如何做到这一点。
数据网格上的SelectionUnit是CellOrRowHeader

我一直在努力尝试从DataGrid.CurrentCell、SelectedCellsChangedEvent处理程序等中提取值,但没有成功。
为什么选择datagrid单元格时获取其当前值如此困难

如果您有任何建议,我们将不胜感激……

不确定您哪里出了问题,但这对我很有用
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



您是否尝试处理DataGridCell.GotFocus事件?因为当你点击一个单元格时,它会收到一个焦点,对吗?另一个单元格中的值如何与组合框中的选定值不同?它是计算的还是什么?@bamamanow:GotFocus事件处理程序与DataGrid.CurrentCell属性具有相同的信息。Ed Plunkett:datagrid中有几个组合框列。textblock单元格可以使用combobox列的任何单元格中的任何值进行更新。问题是在选择组合框单元格时更新文本块,而不是在组合框选定值更改时更新文本块。确定,因此,当选择第0列中的组合单元格时,文本单元格应显示该行第0列中的内容;当选择第1列中的组合单元格时,它应该是该行第1列中的组合单元格。这就是你的意思吗?@EdPlunkett正是。当用户从列表中选择一个新值时,textblock单元格也会随之更新(使用绑定属性可以轻松完成)。
<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>