My application is basically a specialized XML editor. I display the XmlDocument in a TreeView declared as:
<TreeView Name="asifTreeView" ItemTemplateSelector="{StaticResource treeItemTemplateSelector}" SelectedItemChanged="asifTreeView_SelectedItemChanged" PreviewTextInput="asifTreeView_PreviewTextInput" ItemsSource="{Binding Mode=TwoWay, XPath=*}" Loaded="asifTreeView_Loaded" Margin="5"/>
The DataContext is an XmlDataProvider that is created by this code:
Data = new XmlDataProvider(); Data.XPath = "*"; Data.DataChanged += Data_DataChanged;
For XmlElement nodes with a single XmlText node (which hold the data for my application), treeItemTemplateSelector returns this HierarchicalDataTemplate:
<HierarchicalDataTemplate x:Key="textNodeTemplate"><StackPanel Orientation="Horizontal" ToolTip="{Binding Converter={StaticResource xmlNodeToToolTipConverter}}" ContextMenu="{Binding Converter={StaticResource xmlNodeToContextMenuConverter}}"><TextBlock Text="{Binding Path=Name}"/><TextBox Text="{Binding Path=FirstChild.Data, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/></StackPanel></HierarchicalDataTemplate>
In my view model I track the changes to the content of the TextBox by handling the NodeChanged event:
doc.NodeChanged += NodeChangedHandler;
private void NodeChangedHandler(Object src, XmlNodeChangedEventArgs args) { UndoStack.Push(new EditorOperation(args)); }
Then I implement 'Undo' by doing:
public void Perform_Undo() { if (UndoStack.Count > 0) { EditorOperation changeData = UndoStack.Pop(); if (changeData.Action == XmlNodeChangedAction.Change) { changeData.Node.Value = changeData.OldData; } } }
I can see the XmlNodeChanged event fire and I record the change in my UndoStack. When the Perform_Undo() function is called I see the data in the XmlDocument change, but the TextBox in the TreeViewItem is not.
I had an earlier problem with data going the other way (changes made in the TextBox not being propogated to the XmlDocument) but that was fixed by adding UpdateSourceTrigger=PropertyChanged to the ItemTemplate in the HierarchicalDataTemplate.
Can anyone explain why the binding does not seem to be working in this case? How do I fix it?
Thanks,
Bill