MVVM How to make observablecollection<string> always contain a string
I am trying to figure out how do I make an observablecollection of type string always contain a string without manually listing strings. I want the user to be able to click on a button and type in their own string, but allow the observablecollection to automatically detect a string inside the collection of buttons
124 Replies
there's no shortcut, if you want to make sure there's always at least 1 string then you need to add logic to enforce that
like in the operation that removes items from the collection, check if it's empty and if so add your default one back
I did that, I type in a name whenever I add a new item into the collection, and for some reason it never has a string in it. if (!Items.Contains(string) then add something, and it always adds something because of it
would have to see code
Okay
I'll post it here
GitHub
Notepad-Bugged-with-breakpoint-/ViewModel/MainViewModel.cs at maste...
A randomly generated code keeps popping up and will not let me use the app anymore - Morganiscooler/Notepad-Bugged-with-breakpoint-
this is the viewmodel im using that's connected to the page I want to delete items from
i don't see any logic here that will ensure that there's always 1 item in the collection
and there isn't 1 item in the collection to begin with
How so?
there just... isn't?
in your delete command i would expect some logic to check if there are no items left and add one back, or prevent removing a specific one
I don't want to add a new item when I delete something. How can I make sure the item I'm adding in the first place counts as an item in the collection?
i'm getting really confused about what you're trying to do
if you add a string to the collection, it's added
there's nothing else to it
im confused on why the items im creating don't count as items in the collection
and im trying to make them items in the collection
when you use the debugger do you see that the collection contains the items you're adding?
Items.Add(Text);
adds an item, that's all you have to do to make it "count"No
it doesn't contain a string it says
Unless I make string = Text, which isn't what I want to do because the Entry resets
Unless I make string = Text
i don't know what this means
or what an entry isText is binded to an entry box
every single time I go to create a new note, the entry box is blank or changed to something else, since this is the name of the note and not all notes are the same name
which makes deleting previous notes impossible
I am trying to work around this, I want the collection to recognize any string as an item
abc could be an item, hello123 can be another item etc
I want the collection to recognize any string as an item
i'm not playing dumb, but what does this mean
the collection doesn't "recognize" strings
it just has strings in it, if you want to remove one then pass the same text to Remove and it will be goneHow do I get it to recognize the same text every time?
what does recognize mean in this context?
like know the text is in there
so it can be removed
by doing nothing, because it already does that?
Jimmacle
REPL Result: Failure
Exception: CompilationErrorException
Compile: 447.256ms | Execution: 0.000ms | React with ❌ to remove this embed.
sad modix
The item fluxtuates though
like one name could be blah
another one could be hi
Is there a way to get it to be the same string regardless?
...huh?
i'm sorry but i'm totally lost on what you want
can you explain it in plain english, no code?
what do you want to happen from the user's perspective
I'll try
User perspective: As an user, I want to be able to create a note with any given name, but I want to be able to delete any of the notes without forcing them to be the same name every single time
so you have some kind of textbox that holds the name they want to delete yeah?
Oh yeah
so just take that text and remove it from the collection
i don't understand where "forcing them to be the same name" is coming from, because if that's happening that's an issue in your own code
How do i contain the same text they input
...it would already be there
you're trying to remove an existing item from the collection correct?
yes
yeah
so take the text from the user's input, and call
collection.Remove(theUsersText)
Hmm ok
So in this case should I like, not make it binded to the same spot because the user will change the Text when they make their second note
I need the text to remain the same so it's deletable
that doesn't sound related to this
what?
i'm going to do something else for 5 minutes and give you some time to thorougly explain your project and what needs to happen here
The user creates a new note, then creates another note
there should be 2 items in the collection at this point
we should be able to delete any of the 2 items in the collection
But the problem is, I can't delete of any of the 2 items. I can only delete items that contain the same string, like "hi" and "hi" because it is the same as the string Text that the user types in when creating a note
But as soon as they change the Text to something else, "hi" can no longer be deleted
I don't know how to get the collection to contain all the names of the items the user creates, so that any of the items can be deleted
does the collection not just contain names right now?
I'll get a recording of this problem if this doesn't make sense give me a few minutes
or are you trying to put the title and contents of a note in the same string
Yeah it only contains one name at a time
that's not what i asked
It does contain the name you give the note, yes
I need to brb
I'll upload a video of my problem
I'm doing this actually the more I think about it
It contains the name on the cover, with a title and is supposed to contain data within the note when navigating to it
then it sounds like you need a more complex type to represent your note
like a Note class with Title and Body strings
This is what happens when I use delete the notes based off the Text
here's what happens when I click on it (it navigates to a different page)
So I can't just remove a note if it contains any of the names in the collection?
what does the code for the delete button look like
what does that mean
its better to give each note an id and delete by that id
so you can have multiple notes with the same text
I've been thinking about an Id system
But I have no idea how I would be able to implement it
is my discord bugged or is there a giant blank space
oh its gone
anyway lol
think about it
thats what programming is :)
thinking about how to implement stuff
The person said that I need to make a more complex class in order to specifically delete notes
However, I thought the solution would just be deleting an item if it contained a name, like a string "abc"
making a note object is a good idea yes
because a note isnt just the text in the note
I was thinking "why not just store all the strings the user types, and if any of the items contain any of those strings previously written, then make those items deletable"
contained?
not equals?
What's the difference?
The tutorial I watched used contain, but I don't get why equals would be used
you want to delete all notes that contain the note the user wants to delete?
Are there any examples of this being used for multiple buttons? I haven't seen any just yet
forget about the ui for now
how would you identify a note to delete
Not buttons sorry I mean items
Yes
Or better: make anything in the collection that contains any form of string be deletable
So it's up to the user
but when i select a note and click delete i expect only the note that i selected to be deleted
Yeah
so if i have 2 notes
hello
hello world
and i click delete on hello
, both of those would be deleted?Oh wait,so you're saying this wouldn't be specific enough
Nope, just the first
because
hello world
contains hello
Ahh true
So I can't really use contains then
yeah because thats not what you want
Can you tell me more about the equals idea?
It's more specific right
and if i have 2 identical notes
and i click delete on the second one
i dont want the first one to be deleted
right?
Yes
so how would you differentiate between these 2 identical notes?
The id system
how would you identify which is which?
yes you give them ids
Assign a number to each note
yup
Contains doesn't accept ints thoufh
you dont want contains
write in english what you want the delete button to do
the logic of the delete button
shouldnt be that long of a sentence
When the delete button is clicked, I want the item the user selects and deletes to be deleted from the observable collection, and thus hidden on the UI
right but whats the logic
Code now right
an example of a logic:
"find the string in the collection that matches the text of the note, and delete it"
but this logic cant differentiate between identical notes
so whats the logic with ids
Okay
Find the number in the collection that matches the number of the note, then delete it
and where would you get the number of the note?
Hmm
Right now I'm thinking of a count system
Id = 0
While collection > 0, id++
while?
While it grows right?
wdym
Or maybe a for each item in collection
Increment and assign it to each item
why not just assign the id when you create the note
Yeah okay that works
So when you add a new note, the number comes along with the note when it's added right?
a common simple way is to start at 0 and just increment the id whenever you add an object
ok let me ask something else
how would you store the notes?
The notes are stored on the homepage in a growing collectionview
yes but how
You may click on each note and it will eventually navigate to it's own page
are they stored as a strings?
Oh yes
so as far as your app knows, a note is a string
Yeah they are strings, they contain text
Yep
but you dont just want them to be strings right?
you want a note to have an id
and a color
and maybe a title
so string alone isnt enough right?
Yes
I already gave it a title color etc through the UI
and where do you store the title and the color?
This is .NET Maui by the way fyi
I binded them on the UI
to what?
They are apart of the properties
The title will just be the string that the user gives the note
The title property
And the background color property
and where are those properties?
you said earlier theyre stored as strings
dotnet
YouTube
.NET MAUI Data Binding with MVVM & XAML [5 of 8] | .NET MAUI for Be...
Learn more ➡️ https://learn.microsoft.com/training/paths/build-apps-with-dotnet-maui/
View full playlist: https://aka.ms/dotnet/beginnervideos/youtube/maui
Get Started with .NET in Visual Studio: https://aka.ms/dotnet/get-started/vs
Welcome to the .NET MAUI for Beginners Series where you will learn the basics of building multi-platform apps wit...
I followed this tutorial for the storing title and color onto the items added in the collectionview
The data type is a string as shown in the video, and the collection is known as an observable collection of type string
ah
so what theyre doing
By the way I want the delete to work the way it's working for him
But mine isn't a mobile app so I can't use swipe view's delete, and his code isn't working because of the contains as mentioned
is checking if the list contains the item
theyre not deleting any string that contains the string
theyre checking if the string is in the list before deleting
i think you misunderstood that
Ah wait
Let me try to understand
also they didnt implement colors or titles
Yeah I did that for myself
I made my own color functions
Theirs is a task app but similar to mine I guess
right so we go back to my original question then
where are you storing the colors
The colors are stored in the command
When a user clicks on a color box it changes their note background color
This background color is then stored in the mainpage's UI in each item of the collection view
so its only in the ui
what does stored in the commad mean?
you said earlier you were binding them to a property
But the problem is, the notes I make aren't in the list before deleting
I need to make them be in the list
The command changes the observable property of the color to the chosen color, which then I can use that observable color to bind to the background color property
So if the Id helps make the note actually exist in the list so I can delete it then I will implement this
so you have a viewmodel for the notes
Yes
The mainviewmodel is where I store the observable collection, which are the notes
so its an observable collection of the note viewmodel?
It contains all the visible items which are the notes if that's what you're asking
what are the notes
observable collection of what
that what ive been asking
lol
Strings
Sorry
Observable collection<string>
I was confused about the view model part until I saw this
I think you're asking the type the notes are and in that case it would be observablecollection<string>
alright i gotta go but what i was getting at is to make a observable collection of note viewmodels
Note viewmodels?
When you come back could you explain that a bit
I think I'm going to try to do this, because even if I do use contains only one specific note is going to have for example the number "1"
And by this logic I think it should get me to delete specific notes even when another note has the same string in its name