The XML containing the data looks like this:
<Meal name="Mushroom Omelette" type ="Vegetarian, Quick">
<Ingredients>
<Ingredient>Eggs</Ingredient>
<Ingredient>Mushrooms</Ingredient>
<Ingredient>Cheese</Ingredient>
<Ingredient>Salad</Ingredient>
</Ingredients>
</Meal>
<Meal name="Lamb Chops + Veg" type="">
<Ingredients>
<Ingredient>Lamb Chops</Ingredient>
<Ingredient>Brocolli</Ingredient>
<Ingredient>Courgette</Ingredient>
</Ingredients>
</Meal>
The grid looks like this. For each <Meal> element there is a row with text from the name attribute.
Double clicking on the row expands the grid to show the ingredients. You can also edit these
I used the XmlDataProvider for binding, this needed to be in the Windows.Resources to be accessible by the save event.
<Window.Resources>
<!-- Meal Data needs to be in the window resources so it can be accessed easily by the save event -->
<XmlDataProvider x:Name="Meals" x:Key="MealData" Source="./Data/SuperMarketDataMeals.xml" XPath="/SuperMarketData/Meals/Meal"/>
The DataGrid xaml itself is shown below
I use a DataGridTemplate column for the rows, using {Binding XPath=name} for the text.
For the expanded details grid I use a RowDetailsTemplate with its own DataGrid, ItemsSource now with {Binding XPath=Ingredients/Ingredient}
The double click handler simply changes the DetailsVisibility flag on the clicked row:
var row = (DataGridRow)sender;
row.DetailsVisibility = row.DetailsVisibility ==
System.Windows.Visibility.Collapsed ?
System.Windows.Visibility.Visible :
System.Windows.Visibility.Collapsed;
The bug was that when the row was minimised by double clicking, the grid details still contained the expanded row. This was manifested by dragging the next meal down across to the calendar grid. Instead of seeing that meal appear, the meal that had had its details shown was dropped instead. For example from the screen shot above, when the Omelette was minimised, dragging Lamb Chops for example would end up with the Omelette dropped.
To fix this, I needed to refresh the data grid when collapsing the details.
I needed to call CommitEdit first otherwise I would get a runtime exception "Refresh is not allowed during an AddNew or EditItem transaction"
if (row.DetailsVisibility == Visibility.Collapsed)
{
mealGrid.CommitEdit(DataGridEditingUnit.Row, true);
mealGrid.Items.Refresh();
}
No comments:
Post a Comment