C# 无法将“CustomObject”类型的对象强制转换为System.Data.DataRowView异常

C# 无法将“CustomObject”类型的对象强制转换为System.Data.DataRowView异常,c#,wpf,datagrid,datarowview,C#,Wpf,Datagrid,Datarowview,我最近遇到了如下错误: public List<Module> ChosenMods { get; set; } public List<Module> ChosenMods = new List<Module>(); public List<Module> ChosenMods1 { get => ChosenMods; set => ChosenMods = val

我最近遇到了如下错误:

public List<Module> ChosenMods { get; set; }
public List<Module> ChosenMods = new List<Module>();

public List<Module> ChosenMods1
        {
            get => ChosenMods;
            set => ChosenMods = value;
        }
“无法将类型为
pPP_2.Module
的对象强制转换为类型
System.Data.DataRowView

我的代码中有一个Datagrid,它有一个按钮列。像这样:

public List<Module> ChosenMods { get; set; }
public List<Module> ChosenMods = new List<Module>();

public List<Module> ChosenMods1
        {
            get => ChosenMods;
            set => ChosenMods = value;
        }

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

现在是按钮单击事件的代码: (当然,属性是在类的开头声明的,但我在这里展示它们是为了确保连接是清晰的)

公共字符串选择模块{get;set;}
公共列表ChosenMods{get;set;}
private void按钮base_OnClick(对象发送方,RoutedEventTarget e)
{
尝试
{
---DataRowView DataRowView=(DataRowView)((按钮)e.Source).DataContext---
ChosenModule=dataRowView[0]。ToString();
ChosenMods.Add(ChosenModule);
//这是显示按钮点击行数据的代码。谢谢。
}
捕获(例外情况除外)
{
Show(例如Message.ToString());
}
}
现在我的问题是: ButtonBase_OnClick方法应该将所选行的模块传递给字符串列表
ChosenMods
(我得到一个模块的名称,该模块被声明为带有datarowview[0]的字符串)。 当我单击按钮时,会出现异常:

无法将类型为“pPP_2.Module”的对象强制转换为类型为“System.Data.DataRowView”
在我用---标记的行中

我已经找了这么多了,但还是找不到解决问题的办法。。 多亏回顾了我的问题

问候你,伊雷泽。

Sooo 我想没有人会在意,但以下是我自己问题的解决方案:

Xaml部分保持不变。 背后的代码:

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());
                }
            }
        }
我基本上通过
modulemodule=(Module)((Button)e.Source)获得按钮的Datacontext

稍后我将把它放在一个模块列表中,稍后我将重用它

第一个问题是我的列表的初始化,如下所示:

public List<Module> ChosenMods { get; set; }
public List<Module> ChosenMods = new List<Module>();

public List<Module> ChosenMods1
        {
            get => ChosenMods;
            set => ChosenMods = value;
        }
public List ChosenMods{get;set;}
但必须是这样的:

public List<Module> ChosenMods { get; set; }
public List<Module> ChosenMods = new List<Module>();

public List<Module> ChosenMods1
        {
            get => ChosenMods;
            set => ChosenMods = value;
        }
public List ChosenMods=new List();
公共列表ChosenMods1
{
get=>ChosenMods;
set=>ChosenMods=value;
}
第二个问题是,我使用了DataRowView,我意识到它在那里毫无意义

谢谢你的回答,祝你一周愉快


您好,IRezzet

为什么不使用绑定到
SelectedItem
功能
属性?“搜索数小时后,我发现DataRowView是访问存储在DataGrid选定行中的数据的一种方式”-您能分享源代码吗?我有一些WPF的经验,但我不知道。这本参考书对其他需要帮助的读者很有用,我相信是的