C
C#6mo ago
Matze

WPF List

I am trying to implement a Listbox in a WPF Application. My Problem is that i can´t add new items bc i do not understand the shit behind the listbox. Can someone help me
40 Replies
canton7
canton76mo ago
It's hard to answer the question "I don't understand the shit". Specific questions are much more likely to get useful responses What exactly don't you understand?
Pobiega
Pobiega6mo ago
they previously mentioned using databindings, but no further details are known $details
MODiX
MODiX6mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Matze
Matze6mo ago
How can i add items to the list
Pobiega
Pobiega6mo ago
show your code. specifically the list and what it is bound to
Matze
Matze6mo ago
Pobiega
Pobiega6mo ago
those are not bound you have no databindings there
Matze
Matze6mo ago
yeah i have no idea how to do it
Pobiega
Pobiega6mo ago
But I asked "are you using data bindings or not" and you said that you were but you are not.
Matze
Matze6mo ago
my task is to add books with 3 Stages: To be read, in progressed or finished an then the book should go in one of the list
Pobiega
Pobiega6mo ago
Okay There are a few ways to do that You could manually insert and move items around or have them all point to the same source, but with a "filter" in place thats significantly harder to implement thou
Matze
Matze6mo ago
i have made an enum with the 3 stages
Pobiega
Pobiega6mo ago
Right.. Please name your enums appropriately :p ToBeRead InProgress Finished etc
Matze
Matze6mo ago
and on another Ui you can select the Status
Pobiega
Pobiega6mo ago
mhm thats fine.
Matze
Matze6mo ago
sorry that it is in german xD
Pobiega
Pobiega6mo ago
So, you are currently NOT using data bindings. Is this fine and you want to keep not using them, or do you want to use bindings?
Matze
Matze6mo ago
i want to use data binding but i am not quite sure how to do it
Pobiega
Pobiega6mo ago
You don't have to, just fyi. You can easily do this without bindings.
Matze
Matze6mo ago
yeah i know... but my task is to use databinding
Pobiega
Pobiega6mo ago
Okay, great. See, if you had just said this from the start, that would be better.
Matze
Matze6mo ago
yeah sorry i am working in a team and thought they had made it already sorry
Pobiega
Pobiega6mo ago
so, bindings are always relative to the windows "data context" do you currently have a data context at all? Look at your <Window ...> declarationn in the xaml and also in your public partial class MainWindow : Window or whatever your window is called
Matze
Matze6mo ago
i don´t think so
Pobiega
Pobiega6mo ago
check the .xaml.cs file too, to make sure
Matze
Matze6mo ago
Pobiega
Pobiega6mo ago
okay that means your context is the window itself meaning you could in theory bind something to books here
Matze
Matze6mo ago
yeah.. i made a class with all the details to a book
Pobiega
Pobiega6mo ago
okay, those are private fields you'll want public properties for the bindings
Matze
Matze6mo ago
how can i change that?
Pobiega
Pobiega6mo ago
Do you not know what public vs private is? and property vs field for that matter
Matze
Matze6mo ago
not really
Pobiega
Pobiega6mo ago
Then uh.. you need to go back and learn that this is trying to do surgery without knowing what a scalpel is
Matze
Matze6mo ago
ok thanks
Pobiega
Pobiega6mo ago
you will want to read and understand this (very long) documentation: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-7.0#binding-to-collections
Data binding overview - WPF .NET
Learn about the different data sources you can add to your project in Windows Presentation Foundation for .NET. Data sources can be bound to XAML elements to create dynamic apps.
Pobiega
Pobiega6mo ago
I think this is roughly what you wanted, yes? pardon the absolutely terrible user interface
Matze
Matze6mo ago
yeah
Pobiega
Pobiega6mo ago
okay, so the way to do that is with a single list and three "collection views"
Pobiega
Pobiega6mo ago
Stack Overflow
WPF Multiple CollectionView with different filters on same collection
I'm using a an ObservableCollection with two ICollectionView for different filters. One is for filtering messages by some type, and one is for counting checked messages. As you can see message fil...
Matze
Matze6mo ago
thank you very much