c# - 无法将类型为“CustomObject”的对象转换为 System.Data.DataRowView 异常
问题描述
我最近遇到了这样的错误:
“无法将类型的对象pPP_2.Module
转换为类型System.Data.DataRowView
”。
我的代码中有一个 Datagrid,它有一个 Button 列。看起来像这样:
Xaml 代码背后:
<DataGrid x:Name="ModuleGrid" Grid.ColumnSpan="6" Grid.Column="1" Grid.RowSpan="4"
Grid.Row="1" AutoGenerateColumns="False" CanUserAddRows="False" Margin="10,10,10,13" GridLinesVisibility="None" AlternatingRowBackground="DarkGray">
<DataGrid.Columns>
<DataGridTextColumn Header="Module Name" Binding="{Binding Path=Name}"></DataGridTextColumn>
<DataGridTemplateColumn Header="Features">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
<ListView.DisplayMemberPath>
Name
</ListView.DisplayMemberPath>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Min:Values">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
<ListView.DisplayMemberPath>
MinValue
</ListView.DisplayMemberPath>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Max:Values">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
<ListView.DisplayMemberPath>
MaxValue
</ListView.DisplayMemberPath>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Change Per Minute Per Liter">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding Features}" SelectedItem="{Binding Feature}" >
<ListView.DisplayMemberPath>
ChangePerMinutePerLiter
</ListView.DisplayMemberPath>
</ListView>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Click="ButtonBase_OnClick" Background="LightSkyBlue" Margin="5">Pass</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
每行都填充了一个自定义类型模块的对象,后面的代码:
public class Module
{
public string Name { get; set; }
public string UniqueName { get; set; }
public Dictionary<Property, double> Properties { get; set; }
public List<Feature> Features { get; set; }
private Dictionary<Material, double> content = new Dictionary<Material, double>();
public Dictionary<Material, double> Content
{
get
{
return content;
}
set
{
content = value;
}
}
public double FillingCapacity { get; set; }
public double FillingAmount
{
get
{
double ret = 0;
foreach (double a in Content.Values)
{
ret += a;
}
return ret;
}
}
public override string ToString()
{
return Name;
}
}
public class Feature
{
public string Name { get; set; }
public string FeatureType { get; set; }
public Property UsedProperty { get; set; }
public double MinValue { get; set; }
public double MaxValue { get; set; }
public double ChangePerMinutePerLiter { get; set; }
}
)
现在是按钮单击事件的代码:(当然,属性是在类的开头声明的,但我在这里显示它们以确保连接清晰)
public String ChosenModule { get; set; }
public List<String> ChosenMods { get; set; }
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
try
{
---DataRowView dataRowView = (DataRowView)((Button)e.Source).DataContext;---
ChosenModule = dataRowView[0].ToString();
ChosenMods.Add(ChosenModule);
//This is the code which will show the button click row data. Thank you.
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
所以现在我的问题是:ButtonBase_OnClick 方法应该将所选行的模块传递给字符串列表ChosenMods
(我得到一个模块的名称,它被声明为带有 datarowview [0] 的字符串)。当我单击按钮时,我得到异常:
Unable to cast object of type "pPP_2.Module" to type "System.Data.DataRowView"
在我用 --- 标记的行中。
我已经搜索了很多,但我无法找到解决我的问题的方法。感谢查看我的问题,
问候,IRezzet。
解决方案
Sooo I dont think anyone will care but here is my Solution to my own Problem:
The Xaml part stays the same. the Code behind:
private void Button_OnClick(object sender, RoutedEventArgs e)
{
Module module = (Module) ((Button) e.Source).DataContext;
if (ChosenMods.Contains(module))
{
try
{
MessageBox.Show("The Module `" + module.Name + "` is already chosen!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
else
{
try
{
ChosenMods.Add(module);
MessageBox.Show("The Module `" + module.Name + "` has been added!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
I basically get the Datacontext of the Button with the Module module = (Module) ((Button) e.Source).DataContext;
I later to put it inside a List of Modules, which i reuse later.
The first problem was the initialization of my List which looked like this:
public List<Module> ChosenMods { get; set; }
but had to look like this:
public List<Module> ChosenMods = new List<Module>();
public List<Module> ChosenMods1
{
get => ChosenMods;
set => ChosenMods = value;
}
The second problem was, that i used DataRowView which, i realized, was absolutely pointless there.
Thanks for your answers anyway, have a nice Week.
Greetings, IRezzet
推荐阅读
- python - 如何按第二列从大到小对两列的二维数组进行排序?
- python - 改进请求 - Python 脚本
- arrays - 将字符串数组解析为 Int 矩阵
- javascript - 使用 appendto 将 href添加到 img
- rust - 在 rust 中从 HashMap 中删除一个随机条目
- assembly - 在 8086 中的 DS 段的位置之间移动数据
- javascript - 如何将文本文件的内容存储到 vanilla javascript 中的变量中
- css - 显示另一个元素的属性
- c - 将 int16_t 变量转换为 uint8_t 以传递给函数
- ios - Cloud Function annotateImage() 将“位置 1 的 JSON 中的意外标记 o”返回到本机 iOS 应用程序