【问题标题】:How can I handle this cast exception in C# : system.data.model in system.windows.controls.CheckBox?如何在 C# 中处理此强制转换异常:system.windows.controls.CheckBox 中的 system.data.model?
【发布时间】:2020-12-14 06:55:28
【问题描述】:

这是我的分析模型类

public partial class analys
    {
        public int id { get; set; }
        public string name { get; set; }
        public int price { get; set; }
        public bool Type_qualitatif { get; set; }
        public bool Type_quantitatif { get; set; }
        public System.DateTime create_at { get; set; }
        public System.DateTime update_at { get; set; }
        public int sectionId { get; set; }
    
        public virtual section section { get; set; }
    }
}

我创建了一个列表框,它显示复选框为项目,每个项目的内容是一个分析的名称。

所以我试图检查复选框的内容并将它们保存在一个列表中,代码如下

foreach ( CheckBox item in  _biochimieExam.BiochimieExamsItemsControl.Items )
             {
                 if(item.IsChecked == true)
                 {
                   //  String exam = item.Content.ToString();
                     SelctedItems.Add(item.Content.ToString());
                 }
             } 
            
            foreach(String item in SelctedItems)
            {
                BchExamsList.Add(new analys
                {
                    name = item
                }) ;
            }

但我遇到了一个无效的强制转换异常无法在“System.Windows.Controls.CheckBox”类型中强制转换类型为“System.Data.Entity.DynamicProxies.analys_F69488E20B133B223573E1D5931B83214F85574493596933EB8BFDBD5F513C59”的对象。我试图解决。但是我失败了。

我需要一些帮助。谢谢。

【问题讨论】:

    标签: c# checkbox casting system.data


    【解决方案1】:

    在这一行

    foreach ( CheckBox item in _biochimieExam.BiochimieExamsItemsControl.Items )
    

    您试图将_biochimieExam.BiochimieExamsItemsControl.Items 中的每个元素都视为复选框,但_biochimieExam.BiochimieExamsItemsControl.Items 中的元素为analys 类型。

    试试这个:

    foreach (analys item in _biochimieExam.BiochimieExamsItemsControl.CheckedItems)
    {
        BchExamsList.Add(new analys
        {
            name = item.name
        });
    }
    

    【讨论】:

    • 问题是 ListBox 没有跟踪检查的复选框的方法。所以代码有错误_biochimieExam.BiochimieExamsItemsControl.CheckedItems
    • 是否有任何 itemsControl 可以跟踪选中的 CheckBox,因为它是项目?
    • 我忘了在我的回答中提到您实际上应该使用 CheckedListBox,这是另一个具有与 ListBox 相同功能的标准控件以及复选框。
    【解决方案2】:

    我终于通过使用Xceed工具包的CheckListBox解决了这个问题。

    我的目标是获取选中复选框的值。首先,我使用带有 CheckBoxes 的 ListBox 作为它的项目。这就是我写这段代码的原因

    foreach ( CheckBox item in  _biochimieExam.BiochimieExamsItemsControl.Items )
                 {
                     if(item.IsChecked == true)
                     {
                       //  String exam = item.Content.ToString();
                         SelctedItems.Add(item.Content.ToString());
                     }
                 } 
                
                foreach(String item in SelctedItems)
                {
                    BchExamsList.Add(new analys
                    {
                        name = item
                    }) ;
                }
    
    

    为了解决我的问题,我在我的代码中进行了这些更改:

    Xaml

    
     <XceedToolkit:CheckListBox Margin="5,5" x:Name="BiochimieExamsItemsControlXceed" 
                                       ValueMemberPath="name"  DisplayMemberPath="name" FontWeight="Bold"
                                       SelectedMemberPath="name" SelectedValue="{Binding SelectedValue}"  
                                       SelectedItemsOverride="{Binding SelectedItems}"
                                       Style="{DynamicResource ListAnalysesItemsControlXceed}" ItemSelectionChanged="BiochimieExamsItemsControlXceed_ItemSelectionChanged" >
    
                <XceedToolkit:CheckListBox.Resources>
                    <Style TargetType="CheckBox">
                        <Style.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Background" Value="white"></Setter>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="Gray"></Setter>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                                <Setter Property="Background" Value="white"></Setter>
                            </Trigger>
                        </Style.Triggers>
                        <Setter Property="Background" Value="White" />
                        <Setter Property="MinHeight" Value="16"/>
                        <Setter Property="VerticalContentAlignment" Value="Center"/>
                        <Setter Property="Margin" Value="8,2"/>
                        
                        
                     
                  
                    </Style>
                </XceedToolkit:CheckListBox.Resources>
                
            </XceedToolkit:CheckListBox>
    
    

    在后面的代码中:

    public ObservableCollection<Object> BchSelectedExams { get; set; }
    
    
            public void BiochimieExamsItemsControlXceed_ItemSelectionChanged(object sender, Xceed.Wpf.Toolkit.Primitives.ItemSelectionChangedEventArgs e)
            {
               
                BchSelectedExams = (ObservableCollection<object>)BiochimieExamsItemsControlXceed.SelectedItems;
               
    
                foreach (analys o in BiochimieExamsItemsControlXceed.SelectedItems)
                {
                    Console.WriteLine(o.name);
                  
                }
    
    
            }
    
    

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2020-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      相关资源
      最近更新 更多