C
C#5mo ago
MorganIGuess

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