FirstOrDefault - lucyberryhub/WPF.Tutorial GitHub Wiki
๐ FirstOrDefault
Hello, darling! ๐ Iโm Lucy Berry, your Berry-Tastic Guide to all things coding and cuteness! Today, we're going to learn how to search for a cute cherry in a bunch of berries (aka modifying the DataContext in a lovely way with the FirstOrDefault
method)! ๐โจ
Letโs get ready for a super detailed, step-by-step, and fun tutorial that will show you how to search for a cherry and modify its value, just like finding the juiciest berry in a basket! ๐๐
Letโs do this with cherries and berries while making everything look as cute as possible! ๐๐
Step 1: The Cherry Button ๐
Imagine that you have a basket full of sweet little cherries ๐, and you want to click on one to change its image! First, we need a Button to represent the cute cherry. When you click on this cherry, weโll change it and also look for another cherry to update. ๐
In this adorable function, weโll be clicking on the Delete Cherry button ๐ and making two things happen:
- Change the clicked cherryโs image.
- Find another cherry (with
Id + 6
) and update its image too!
Here is how weโre going to do this, step-by-step:
Step 2: Code Explanation ๐๐
Letโs look at the code! (Don't worry, itโs all cherry related! ๐)
private void OnDeleteCherryButtonClick(object sender, RoutedEventArgs e)
{
// Step 1: Check if the sender is a button and if its DataContext is a cherry (a cute berry object!)
if (sender is Button cherryButton && cherryButton.DataContext is CherryBerryModel selectedCherry)
{
// Step 2: Change the image of the clicked cherry ๐
byte[] cherryImageBytes = Convert.FromBase64String(AppDbConfig.CherryPlusImage);
selectedCherry.Image = new BerryBlob(cherryImageBytes); // Give it a cute new image!
// Step 3: Look for another cherry with the ID that's 6 more than the current cherry ๐
var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6);
// Step 4: If we find the cherry, change its image to a cute empty image ๐
if (targetCherry != null)
{
byte[] emptyCherryImageBytes = Convert.FromBase64String(AppDbConfig.EmptyCherryImage);
targetCherry.Image = new BerryBlob(emptyCherryImageBytes); // Give it an empty image!
}
}
}
Step 3: Letโs Break Down the Code! ๐
Step 1:
We start by checking if the sender (the thing that triggered the event) is a Button (a cute cherry button ๐). We also check if the DataContext of the button is a cherry object (letโs call it CherryBerryModel
). If these things are true, it means we clicked on a cute cherry, and we can change its image! ๐
sender is Button cherryButton
: We're checking if we clicked on a button (the cherry).cherryButton.DataContext is CherryBerryModel selectedCherry
: This checks if the data tied to the button is a cherry berry object (CherryBerryModel
).
Step 2:
Now, letโs give our selected cherry a lovely new image! ๐ Weโll get the image from the AppDbConfig
and convert it to a byte array (because the image is stored in base64 format, like a yummy candy). Then, we set this byte array to the selected cherryโs image. โจ
Convert.FromBase64String(AppDbConfig.CherryPlusImage)
: This converts the cherry image from base64 into byte data.selectedCherry.Image = new BerryBlob(cherryImageBytes)
: This assigns the new image to the cherry object using a BerryBlob (a special container for images!).
Step 3:
Next, we want to search for another cherry in our basket ๐ (or collection) to update its image. We use the FirstOrDefault
method to find the first cherry in the basket that has an ID that is 6 more than the current cherryโs ID! ๐ If we find that cherry, weโll change its image to a new image that is empty. ๐
var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6)
: This searches for a cherry withId = selectedCherry.Id + 6
.
Step 4:
If we find that special cherry ๐, we change its image to an empty image (for when a cherry is no longer special). ๐
Convert.FromBase64String(AppDbConfig.EmptyCherryImage)
: This loads the empty cherry image.targetCherry.Image = new BerryBlob(emptyCherryImageBytes)
: This assigns the empty image to the found cherry.
Step 4: Putting It All Together ๐
Now that weโve learned the basics, letโs put everything in the cherry jar and make sure the code is super sweet and professional:
private void OnDeleteCherryButtonClick(object sender, RoutedEventArgs e)
{
if (sender is Button cherryButton && cherryButton.DataContext is CherryBerryModel selectedCherry)
{
// Change the selected cherry's image to a "Plus" cherry
byte[] cherryImageBytes = Convert.FromBase64String(AppDbConfig.CherryPlusImage);
selectedCherry.Image = new BerryBlob(cherryImageBytes);
// Find the cherry with the ID + 6 and change its image to "Empty" cherry
var targetCherry = cherryBasket.FirstOrDefault(cherry => cherry.Id == selectedCherry.Id + 6);
// If the cherry exists, set the image to "Empty"
if (targetCherry != null)
{
byte[] emptyCherryImageBytes = Convert.FromBase64String(AppDbConfig.EmptyCherryImage);
targetCherry.Image = new BerryBlob(emptyCherryImageBytes);
}
}
}
Step 5: Your Cherry Berry Journey ๐
Congratulations, sweetie! ๐ Youโve just learned how to search for and modify cherries in your basket (or your DataContext
). You now know how to change the image of the cherry you clicked and even update another cherry by its Id + 6! ๐ Isnโt that just berry fun and cute? ๐