This is my model class of analys
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; }
}
}
I create a ListBox which display checkboxes as items, and the content of each one is the name of an analys.
So I am trying to get the content of the checkboxes checked and save them in a list with the code bellow
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
}) ;
}
But I'm getting an inavlid cast exception Impossible to cast an object of type 'System.Data.Entity.DynamicProxies.analys_F69488E20B133B223573E1D5931B83214F85574493596933EB8BFDBD5F513C59' in type 'System.Windows.Controls.CheckBox'.' which I try to resolve. But a I failed.
Please I need some help. Thanks.
in this line
foreach ( CheckBox item in _biochimieExam.BiochimieExamsItemsControl.Items )
you are trying to treat every element in _biochimieExam.BiochimieExamsItemsControl.Items
as a Checkbox, but elements in _biochimieExam.BiochimieExamsItemsControl.Items
ar of type analys
.
try this instead:
foreach (analys item in _biochimieExam.BiochimieExamsItemsControl.CheckedItems)
{
BchExamsList.Add(new analys
{
name = item.name
});
}
I finally resolve the problem by using the CheckListBox of Xceed toolkit package.
My objective was to get the value of checked checkBoxes. In first I was using a ListBox with CheckBoxes as it's items. And that is why I write this code
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
}) ;
}
To resolve my problem I make these changes in my code:
the 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>
And in the code behind:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.