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
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
would have to see code
CreamofSumYungGai
Okay I'll post it here
CreamofSumYungGai
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-
CreamofSumYungGai
this is the viewmodel im using that's connected to the page I want to delete items from
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
How so?
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
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?
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
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"
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
Unless I make string = Text i don't know what this means or what an entry is
CreamofSumYungGai
Text 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
Jimmacle
Jimmacle4d ago
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 gone
CreamofSumYungGai
How do I get it to recognize the same text every time?
Jimmacle
Jimmacle4d ago
what does recognize mean in this context?
CreamofSumYungGai
like know the text is in there so it can be removed
Jimmacle
Jimmacle4d ago
by doing nothing, because it already does that?
MODiX
MODiX4d ago
Jimmacle
REPL Result: Failure
var collection = new ObservableCollection<string>();
collection.Add("My Item");
collection.Remove("My Item");
Console.WriteLine(collection.Count);
var collection = new ObservableCollection<string>();
collection.Add("My Item");
collection.Remove("My Item");
Console.WriteLine(collection.Count);
Exception: CompilationErrorException
- The type or namespace name 'ObservableCollection<>' could not be found (are you missing a using directive or an assembly reference?)
- Argument 1: cannot convert from 'method group' to 'System.ReadOnlySpan<char>'
- The type or namespace name 'ObservableCollection<>' could not be found (are you missing a using directive or an assembly reference?)
- Argument 1: cannot convert from 'method group' to 'System.ReadOnlySpan<char>'
Compile: 447.256ms | Execution: 0.000ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle4d ago
sad modix
CreamofSumYungGai
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?
Jimmacle
Jimmacle4d ago
...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
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
so you have some kind of textbox that holds the name they want to delete yeah?
CreamofSumYungGai
Oh yeah
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
How do i contain the same text they input
Jimmacle
Jimmacle4d ago
...it would already be there you're trying to remove an existing item from the collection correct?
CreamofSumYungGai
yes
Jimmacle
Jimmacle4d ago
yeah so take the text from the user's input, and call collection.Remove(theUsersText)
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
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
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
does the collection not just contain names right now?
CreamofSumYungGai
I'll get a recording of this problem if this doesn't make sense give me a few minutes
Jimmacle
Jimmacle4d ago
or are you trying to put the title and contents of a note in the same string
CreamofSumYungGai
Yeah it only contains one name at a time
Jimmacle
Jimmacle4d ago
that's not what i asked
CreamofSumYungGai
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
Jimmacle
Jimmacle4d ago
then it sounds like you need a more complex type to represent your note like a Note class with Title and Body strings
CreamofSumYungGai
This is what happens when I use delete the notes based off the Text
CreamofSumYungGai
here's what happens when I click on it (it navigates to a different page)
No description
CreamofSumYungGai
So I can't just remove a note if it contains any of the names in the collection?
sibber
sibber4d ago
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
CreamofSumYungGai
I've been thinking about an Id system But I have no idea how I would be able to implement it
sibber
sibber4d ago
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
CreamofSumYungGai
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"
sibber
sibber4d ago
making a note object is a good idea yes because a note isnt just the text in the note
CreamofSumYungGai
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"
sibber
sibber4d ago
contained? not equals?
CreamofSumYungGai
What's the difference? The tutorial I watched used contain, but I don't get why equals would be used
sibber
sibber4d ago
you want to delete all notes that contain the note the user wants to delete?
CreamofSumYungGai
Are there any examples of this being used for multiple buttons? I haven't seen any just yet
sibber
sibber4d ago
forget about the ui for now how would you identify a note to delete
CreamofSumYungGai
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
sibber
sibber4d ago
but when i select a note and click delete i expect only the note that i selected to be deleted
CreamofSumYungGai
Yeah
sibber
sibber4d ago
so if i have 2 notes hello hello world and i click delete on hello, both of those would be deleted?
CreamofSumYungGai
Oh wait,so you're saying this wouldn't be specific enough Nope, just the first
sibber
sibber4d ago
because hello world contains hello
CreamofSumYungGai
Ahh true So I can't really use contains then
sibber
sibber4d ago
yeah because thats not what you want
CreamofSumYungGai
Can you tell me more about the equals idea? It's more specific right
sibber
sibber4d ago
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?
CreamofSumYungGai
Yes
sibber
sibber4d ago
so how would you differentiate between these 2 identical notes?
CreamofSumYungGai
The id system
sibber
sibber4d ago
how would you identify which is which? yes you give them ids
CreamofSumYungGai
Assign a number to each note
sibber
sibber4d ago
yup
CreamofSumYungGai
Contains doesn't accept ints thoufh
sibber
sibber4d ago
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
CreamofSumYungGai
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
sibber
sibber4d ago
right but whats the logic
CreamofSumYungGai
Code now right
sibber
sibber4d ago
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
CreamofSumYungGai
Okay Find the number in the collection that matches the number of the note, then delete it
sibber
sibber4d ago
and where would you get the number of the note?
CreamofSumYungGai
Hmm Right now I'm thinking of a count system Id = 0 While collection > 0, id++
sibber
sibber4d ago
while?
CreamofSumYungGai
While it grows right?
sibber
sibber4d ago
wdym
CreamofSumYungGai
Or maybe a for each item in collection Increment and assign it to each item
sibber
sibber4d ago
why not just assign the id when you create the note
CreamofSumYungGai
Yeah okay that works So when you add a new note, the number comes along with the note when it's added right?
sibber
sibber4d ago
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?
CreamofSumYungGai
The notes are stored on the homepage in a growing collectionview
sibber
sibber4d ago
yes but how
CreamofSumYungGai
You may click on each note and it will eventually navigate to it's own page
sibber
sibber4d ago
are they stored as a strings?
CreamofSumYungGai
Oh yes
sibber
sibber4d ago
so as far as your app knows, a note is a string
CreamofSumYungGai
Yeah they are strings, they contain text Yep
sibber
sibber4d ago
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?
CreamofSumYungGai
Yes I already gave it a title color etc through the UI
sibber
sibber4d ago
and where do you store the title and the color?
CreamofSumYungGai
This is .NET Maui by the way fyi I binded them on the UI
sibber
sibber4d ago
to what?
CreamofSumYungGai
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
sibber
sibber4d ago
and where are those properties? you said earlier theyre stored as strings
CreamofSumYungGai
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...
CreamofSumYungGai
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
sibber
sibber4d ago
ah so what theyre doing
CreamofSumYungGai
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
sibber
sibber4d ago
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
CreamofSumYungGai
Ah wait Let me try to understand
sibber
sibber4d ago
also they didnt implement colors or titles
CreamofSumYungGai
Yeah I did that for myself I made my own color functions Theirs is a task app but similar to mine I guess
sibber
sibber4d ago
right so we go back to my original question then where are you storing the colors
CreamofSumYungGai
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
sibber
sibber4d ago
so its only in the ui what does stored in the commad mean? you said earlier you were binding them to a property
CreamofSumYungGai
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
sibber
sibber4d ago
so you have a viewmodel for the notes
CreamofSumYungGai
Yes The mainviewmodel is where I store the observable collection, which are the notes
sibber
sibber4d ago
so its an observable collection of the note viewmodel?
CreamofSumYungGai
It contains all the visible items which are the notes if that's what you're asking
sibber
sibber4d ago
what are the notes observable collection of what that what ive been asking lol
CreamofSumYungGai
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>
sibber
sibber4d ago
alright i gotta go but what i was getting at is to make a observable collection of note viewmodels
CreamofSumYungGai
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