1. You cannot access the grid by name as there are multiple dynamic grids and there cannot be multiple fields with the same name (of course that would not help if it was possible).
2. You cannot modify the Items
while the ItemsSource
is bound, but you can change the ItemsSource
, here is an example of how to do that:
<TabControl Name="_tabControl">
<TabControl.Resources>
<XmlDataProvider x:Key="data" XPath="GPA/Semestre"
Source="http://pastebin.com/raw.php?i=JgyYkn4E" />
</TabControl.Resources>
<!-- ... -->
<TabControl.ContentTemplate>
<DataTemplate>
<StackPanel>
<DataGrid Name="localDG" ItemsSource="{Binding XPath=Cadeiras/Cadeira}"
AutoGenerateColumns="False" Tag="{Binding XPath=Cadeiras}">
<DataGrid.Columns>
<!-- ... -->
</DataGrid.Columns>
</DataGrid>
<Button Content="Add Row" Click="AddRow_Click" Tag="{x:Reference localDG}" />
</StackPanel>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
private void AddRow_Click(object sender, RoutedEventArgs e)
{
var data = _tabControl.Resources["data"] as XmlDataProvider;
var dg = (sender as FrameworkElement).Tag as DataGrid; //Reference to DataGrid is stored in the Button.Tag
var cadeiras = dg.Tag as XmlNode; //Used DataGrid.Tag to store parent node of cadeiras so a new cadeira can be added
var newItem = data.Document.CreateElement("Cadeira");
Action<string,string> addProperty = (name, val) =>
{
var prop = data.Document.CreateElement(name);
prop.InnerText = val;
newItem.AppendChild(prop);
};
addProperty("Activa","1");
addProperty("Nome","Lorem Ipsum");
addProperty("Nota","1.3");
cadeiras.AppendChild(newItem);
}
3. Using similar methods as in 2. you can query the items for that property (with LINQ for example)
On a side-note: If there is any chance that you will do just a little bit more manipulation than this i would recommend deserializing the XML to proper CLR-objects. Document object model manipulations are a pain just in terms of additinal lines of code, they are not safe in any way, shape or form, and a nightmare to maintain.