<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;