Get_DataGrid_Cell - lucyberryhub/WPF.Tutorial GitHub Wiki
Hello, my little cherry blossoms! πΈ Today, Lucy Berry learned how to get the DataGrid row information when clicking on a specific cell! Isn't that berry-cute? π Let's dive into this adorable journey together! β¨
We first create our berrylicious columns in the SetColumns
method. Each column has a special role, like being a cute little textbox or a sweet template column for images and text. Here's how the berry magic happens:
private void BerryTable_SetCherryColumns() // π Renamed for cuteness!
{
Style cherryCenteredTextStyle = TemplateHelper.CreateCenteredStyle(typeof(TextBlock));
Style cherryCenteredEditingTextStyle = TemplateHelper.CreateCenteredStyle(typeof(TextBox));
// Define our adorable cherry columns! π
var cherryColumns = new List<DataGridColumn>
{
new DataGridTextColumn
{
Header = "π Id", // It's a hidden berry! πΈ
Binding = new Binding("BerryId") { Mode = BindingMode.OneWay },
Visibility = Visibility.Collapsed
},
new DataGridTextColumn
{
Header = "π Title β ",
Binding = new Binding("BerryTitle") { Mode = BindingMode.TwoWay },
Width = 70,
ElementStyle = cherryCenteredTextStyle,
EditingElementStyle = cherryCenteredEditingTextStyle
},
new DataGridTextColumn
{
Header = "π Title β‘",
Binding = new Binding("BerryTitle2") { Mode = BindingMode.TwoWay },
Width = 70,
ElementStyle = cherryCenteredTextStyle,
EditingElementStyle = cherryCenteredEditingTextStyle
},
new DataGridTemplateColumn
{
Header = (string)Application.Current.Resources["str_cherryBlossomEikaku"],
Width = 70,
CellTemplate = new DataTemplate
{
VisualTree = CreateCherryImageCellTemplate("CherryEikaku")
},
CellEditingTemplate = new DataTemplate
{
VisualTree = CreateCherryTextBoxEditingTemplate("CherryEikaku")
}
},
new DataGridTemplateColumn
{
Header = (string)Application.Current.Resources["str_cherryBlossomDonkaku"],
Width = 70,
CellTemplate = new DataTemplate
{
VisualTree = CreateCherryImageCellTemplate("CherryDonkaku")
},
CellEditingTemplate = new DataTemplate
{
VisualTree = CreateCherryTextBoxEditingTemplate("CherryDonkaku")
}
}
};
// Cherry Table Settings π
BerryTableControl.RowHeight = 60;
BerryTableControl.NeedDeleteButton = true; // Gotta prune those cherries! πΈ
BerryTableControl.SetColumns(cherryColumns);
}
Now, letβs add the cutie-pie method to find out which cherry cell was clicked and retrieve the data. We'll handle the MouseLeftButtonUp
event in our berry-tastic table. Hereβs the magic:
private void BerryTable_CherryMouseClick(object sender, MouseButtonEventArgs e) // πΎ So cute!
{
// Close the CherryPickerPopup by default π
CherryPickerPopup.IsOpen = false;
// Use the cherry tree helper πΈ
DependencyObject clickedBerry = e.OriginalSource as DependencyObject;
while (clickedBerry != null && !(clickedBerry is DataGridCell))
{
clickedBerry = VisualTreeHelper.GetParent(clickedBerry);
}
if (clickedBerry is DataGridCell berryCell)
{
// Find which berry column it belongs to π
var cherryColumn = berryCell.Column;
if (cherryColumn is DataGridTemplateColumn cherryTemplateColumn && cherryTemplateColumn.Header != "")
{
// Get the rowβs data context (the berry basket! π)
var berryBasket = berryCell.DataContext;
if (berryBasket != null)
{
// Retrieve the juicy cherry value! π
var cherryValue = berryBasket.GetType().GetProperty("CherryDonkaku")?.GetValue(berryBasket);
// Open the cherry picker popup πΈ
CherryPickerPopup.IsOpen = true;
// Display berry value (optional)
//MessageBox.Show($"Clicked cherry column: {cherryTemplateColumn.Header}\nBerry value: {cherryValue}", "Berry Clicked!");
}
}
}
}
Donβt forget to add a cute CherryPickerPopup in your XAML to display something berry-adorable! π Hereβs an example:
<Popup x:Name="CherryPickerPopup"
StaysOpen="False"
Placement="Mouse"
Width="150"
Height="50">
<Border BorderBrush="Pink" BorderThickness="2" Background="LightPink" CornerRadius="10">
<TextBlock Text="π Pick your cherry!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="DarkRed" />
</Border>
</Popup>
-
π Cherry DataGrid Setup:
- Create adorable
DataGridTemplateColumn
with customCellTemplate
andCellEditingTemplate
.
- Create adorable
-
π Cherry Cell Click Detection:
- Use
MouseLeftButtonUp
to traverse the visual cherry tree and find the clicked cell. - Retrieve the rowβs data using the
DataContext
.
- Use
-
πΈ Cherry Picker Popup:
- Add a berry-cute popup to show when a special cherry is clicked!
Congratulations, my little cherry blossoms! πΈ Youβve learned how to click on a DataGrid cell and retrieve its berry-sweet data! Keep being berry-tastic, and I'll see you in the next cutie tutorial! πβ¨