gpt4 book ai didi

c# - Datagrid 单元格点击事件

转载 作者:行者123 更新时间:2023-11-30 22:26:46 25 4
gpt4 key购买 nike

<UserControl x:Class="DDCUI.CommDiagnosisWPFCtrl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Height="800" Width="300">
<StackPanel>
<DataGrid MinHeight="300" MaxHeight="600" AutoGenerateColumns="False" Name="DGComm" CanUserResizeColumns="True" IsReadOnly="True" ItemsSource="{Binding Source=dataGridRows}">
<DataGrid.Columns>
<DataGridTextColumn Header="No." Binding="{Binding Number}" Width="0.1*"/>
<DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="0.1*" />
<DataGridTextColumn Header="Protocol" Binding="{Binding Protocol}" Width="0.15*" />
<DataGridTextColumn Header="Source" Binding="{Binding Source}" Width="0.15*" />
<DataGridTextColumn Header="Destination" Binding="{Binding Destination}" Width="0.15*" />
<DataGridTextColumn Header="Data" Binding="{Binding Data}" Width="0.5*" />
</DataGrid.Columns>
</DataGrid>
<RichTextBox Height="150" Name="RtbHexCode"/>
<TreeView Height="200" Name="TreeViewDecode"/>

</StackPanel>
</UserControl>
private void DGComm_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
//e.AddedCells[0].Column
IList<DataGridCellInfo> cells = e.AddedCells;
foreach (DataGridCellInfo di in cells)
{
DataRowView dvr = (DataRowView)di.Item;
MessageBox.Show(di.ToString());
}
}

我希望能够操作选定的单元格。我正在尝试打印单击的单元格的名称,但它在 DataRowView dvr = (DataRowView)di.Item; 上引发了无效的转换异常;说明我无法将 DataSource 转换为 RowView。

我该如何解决这个问题?

编辑:Itemsources 由

public ObservableCollection<object> dataGridRows = new ObservableCollection<object>();

private void InitProtocolParsers()
{
DGComm.ItemsSource = dataGridRows;

最佳答案

您正在通过 di.Item 访问的对象不是 DataRowView 类型但是您要绑定(bind)的实际业务对象。所以无论你在 ObservableCollection<object> 中输入什么可以通过di.Item"访问.

试试吧

MessageBox.Show(di.Item.ToString()) 

我希望这会变得更加清晰。

关于c# - Datagrid 单元格点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623434/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com