Avatar of ajk825
ajk825
 asked on

Please help with filtering observablecollection using checkboxes

Hello. i am very new to wpf and entity framework and I've ran into a snag...I'm using ado.net entity data model. i have an entity, generated from a database.... BoxEntity.  this entity is made up of the tables in my database. there are 8 tables total.

1 parent table,  that is my:

 <CollectionViewSource x:Key="RcViewSource" />

7 of the tables in my BoxEntity are child tables.  these are my :

        <CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Specs'}" x:Key="SpecsViewSource"/>
        <CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Holds'}" x:Key="HoldsViewSource"/>
        <CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Lifes'}" x:Key="LifesViewSource"/>
        <CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='ToDs'}" x:Key="ToDsViewSource"/>
        <CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Crs'}" x:Key="CrsViewSource"/>

1 table i haven't done anything with yet.

5 of the tables/views (shown above) are all checkboxes/ booleans/  true/false answers, no input required but check a box for true, leave it blank for false.

 I tried to find another way to do it, but they are all yes/no columns. And i didn't want to use RadioButtons, just not fond of the way they look.

so... I need to sort/filter the main view (RcViewSource) by the other 5 views. so  i have a window something like this:

This is how my Search form is set up....
 and if the user selects Appetizers, Breakfast, and Vegan..... all the recipe names containing those 3 items will show up in the listbox when they hit the "Search" button.

I"ve attached a copy of my SearchMe.xaml.vb code behind. As you can see I hard coded the button to search for Appetizers when clicked, but their has to be an easier, more efficient way to do the filtering.

I've also attached a copy of my RecipeList, this is my ObservableCollection, as you can see I'm very lost.. And their are NO resources on the web dealing with checkbox bindings, unless you want to change the color of the text .....

Last thing is I need to pass the value of the Item Selected to another window to display the actual recipe.

Or... If there is a way I could write a SQL stored procedure to do all the above that would be great also.....

This has been giving me problems for about 3 months now. Please someone help....
Any and all input or suggestions would be extremely appreciated...
Thanks, Adam
RecipeList.txt
SearchMe-form.txt
Visual Basic.NETMicrosoft SQL Server 2008VB Script

Avatar of undefined
Last Comment
ajk825

8/22/2022 - Mon
Bob Learned

If you want the check boxes to be filters, then I would suggest a dynamic Where clause for a LINQ expression.

Dynamically Composing Expression Predicates
http://www.albahari.com/nutshell/predicatebuilder.aspx
ajk825

ASKER
Ok I'm sorry I've looked over the post you linked your answer to and I've googled it, yahoo searched it, and you tubed it, and i think the closest example to pull off what you suggested is this: Learn C sharp - Functions and Lambda Expressions

But I'm still confused. I have 16 check boxes total that span over 3 different views. How would I write this expression to search and return results whether 1 checkbox is selected or all 16?

I'm sorry. I'm not trying to be ignorant and I do appreciate the help. Only answer to this question I've gotten in weeks. So thank you for that. But a little further assistance would be extremely appreciated.

Thanks.
Adam
Bob Learned

Adam,

I have 16 check boxes total that span over 3 different views
Can you explain how this statement relates to what you showed earlier?  I can't connect the dots yet...
Your help has saved me hundreds of hours of internet surfing.
fblack61
ajk825

ASKER
Hi. Sure, I'm sorry...
 
"I have 16 check boxes total that span over 3 different views...."

In the initial question I showed an image. This image is the search form that will be used in the program I am trying to accomplish. The image shows 15 checkboxes, actually there are 16 total.

3 of them are part of my  "LifesViewSource"

<CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Lifes'}" x:key ="LifesViewSource"/> which is associated with my LifesTbl in my database.

7 of them are part of my "CrseViewSource"

<CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='Crs'}" x:Key="CrsViewSource"/> which is associated with my CrsTbl in my database.

The other 6  are part of my  "ToDsViewSource"

<CollectionViewSource Source="{Binding Source={StaticResource RcViewSource},Path='ToDs'}" x:Key="ToDsViewSource"/> which is associated to my ToDTbl...

What I need is for the user to be able to search my database using any variety of the checkboxes I've supplied (whether it's 1 or all 16) to be able to filter/ sort  the main view (RcViewSource) by the other views. So if the user selects Appetizers, Breakfast, and Vegan   ........(Which,. Appetizers is part of CrsViewSource, Breakfast,  is part of ToDViewSource. and Vegan,  is part of LifeStyleViewSource)..... all the recipe names containing those 3 items will show up in the listbox when they hit the "Search" button.

I've already figured out how to pass the value to the other form, so that part is good. It's  writing this expression to search and return results whether 1 checkbox is selected or all 16  I'm struggling with. Sorry about the poor explanation before. Hope this cleared things up.

Thanks....Adam
Bob Learned

Are you using Entity Framework to get data?  

Do you have a data model for LifesTbl, CrsTbl, ToDTbl (how are they related)?
ajk825

ASKER
Yes I am.... I have a DataModel ("BxEntities').... I do not have one for the others (LifesTbl, CrsTbl, ToDTbl). They are related through the use of  primary/ foreign keys...

My DataModel ("BxEntities") consists of:

RcTbl:
RcID int PK
RcName nvarchar (100)

CrsTbl
CrsID int PK
RcID int FK
Appetizers bit
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bob Learned

If you have a data model, "BxEntities", then how are those check box elements used to filter that data?
ajk825

ASKER
That is what I'm trying to ask. How can I make this work? Ok. Above is my model (except there are 6 more tables all set the same way. PK/FK)
I need to hook up the checkboxes in my search page so that if a user checks them RcViews (that's my RcTbl) will filter by appropriate columns (ie:Appetizers, Vegan, Breakfast) and the end result will display the appropriate Name in a Listview when the "Search" button is pressed.
Bob Learned

I am not talking about physical relationship, but logical relationship.  It sounded like you needed to take all the checkbox options, and build a dynamic where clause for the entities that you get from BxEntities, but I don't know how that would be achieved logically.  I am trying to get a good description for the entire data model, without worrying about how to implement.

Keep in mind that while you have intimate knowledge of your entities, I have very little, so I need to ask probing questions in order to provide the information that you need to achieve your goal.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ajk825

ASKER
I understand that. And I appreciate your help. But please understand I've been working on this project for 3 years. I've taken some computer classes but can't afford to finish my degree. So I've been teaching myself through various web sites, books, help forums,MSDN's sites, and believe it or not Youtube has been very helpful. And I've redone this thing so many times it makes me ill.

So I've included a screen capture of my data model if that helps.
Any other questions I'll do my best to answer.
It sounded like you needed to take all the checkbox options, and build a dynamic where clause for the entities that you get from BxEntities
And that is the response you initially gave right? Dynamically Composing Expression Predicates....
Screen.png
Bob Learned

If you are learning, then might I suggest a small improvement?

The entity model is meant to map directly to your "real world" problem space, so names should make sense in that context.  In the database world, names like RecipeTbl and DirectionsTbl make sense, but in your real world example, names like Recipes and Directions make more sense.  It is pretty easy to rename these physical database elements to logical model elements.

This is the simplified data model that I see:

Recipes
     Courses
     Directions
     Holidays
     Ingredients
     LifeStyles
     SpecialNeeds
     

Which entity is used for the check box text?
ajk825

ASKER
Which entity is used for the check box text?
Not quite sure what ya mean by their text?

CoursesTbl is under EntitySetName CoursesTbls.... Appetizers is in that.

I have : <CheckBox Content="Appetizers" FontSize="18" HorizontalAlignment="Left" Margin="10,10,0,112" Name="AppetizerCB" Width="113" IsChecked="{Binding Path=Appetizers, Mode=TwoWay}"/>

If you are learning, then might I suggest a small improvement?
I'm trying.
screen.png
screen1.png
screen2.png
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ajk825

ASKER
Thank you very much for the patience and guidance. I really appreciate it.