我最近遇到了这样的错误:
“无法将类型的对象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。