无法将类型为CustomObject的对象转换为System.Data.DataRowView异常

时间:2020-02-02 14:05:57

标签: c# wpf datagrid datarowview

我最近遇到了这样的错误:

“无法将类型为pPP_2.Module的对象转换为类型为System.Data.DataRowView的对象”。

我的代码中有一个Datagrid,其中有一个Button列。看起来像这样:

enter image description here

后面的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]声明为String的模块的名称)。 当我单击按钮时,出现异常:

我用---标记的行中的

Unable to cast object of type "pPP_2.Module" to type "System.Data.DataRowView"

我已经搜索了很多东西,但是找不到解决我问题的方法。 感谢审查我的问题,

问候,艾瑞泽特。

1 个答案:

答案 0 :(得分:0)

太棒了 我认为没有人会在意,但这是我对自己的问题的解决方案:

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

我基本上是使用Module module = (Module) ((Button) e.Source).DataContext;

来获取Button的数据上下文的

稍后我将其放入模块列表中,稍后再使用。

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

public List<Module> ChosenMods { get; set; }

但必须看起来像这样:

public List<Module> ChosenMods = new List<Module>();

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

第二个问题是,我使用了DataRowView,我意识到那里绝对没有意义。

仍然感谢您的回答,祝您度过愉快的一周。

问候,艾瑞泽特