Answered by:
How to Bind TreeView to XML in CurrentItem in CollectionView

Question
-
I am having several issues trying to bind a treeview control to XML
Here is a sample of the XML I am tyring to load
<ChangeNode Description="" ChangeType="Change" Type="Settings">
<ChangeDetails>
<ChangeDetailNode Description="TopComment" OldValue="Top Comment" NewValue="Top"/>
<ChangeDetailNode Description="BottomComment" OldValue="Bottom Comment" NewValue="Bottom"/>
</ChangeDetails>
<Children />
</ChangeNode>
Thsi information is contained in a field in the database and accessed through a prioperty of my model through the viewmodel.
Below is the xaml for my view
<UserControl x:Class="AuditReportingView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="562" d:DesignWidth="880" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" xmlns:filter="clr-namespace:DataGridFilterLibrary;assembly=DataGridFilterLibrary"> <UserControl.Resources> <DataTemplate DataType="ChangeNode"> <StackPanel Margin="6" Orientation="Horizontal" > <TextBlock Text="{Binding XPath=@Type}" /> <TextBlock Text="{Binding XPath=@ChangeType}" /> <TextBlock Text="{Binding XPath=@Description}" /> </StackPanel> </DataTemplate> <DataTemplate DataType="ChangeDatailNode"> <StackPanel Margin="6" Orientation="Horizontal" > <TextBlock Text="{Binding XPath=@Description}" /> <TextBlock Text="{Binding XPath=@OldValue}" /> <TextBlock Text="{Binding XPath=@NewValue}" /> </StackPanel> </DataTemplate> <HierarchicalDataTemplate DataType="ChangeDetails" ItemsSource="{Binding XPath=*}"> <TextBlock Text="Change Information"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="Children" ItemsSource="{Binding XPath=*}"> <TextBlock Text="Details"/> </HierarchicalDataTemplate> </UserControl.Resources> <Grid Width="881" Height="559"> <DataGrid filter:DataGridExtensions.UseBackgroundWorkerForFiltering="True" filter:DataGridExtensions.IsClearButtonVisible="false" filter:DataGridExtensions.IsFilterVisible="True" ColumnHeaderStyle="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl}, ResourceId=DataGridHeaderFilterControlStyle}}" AutoGenerateColumns="False" DataContext="{Binding Path=Items}" ItemsSource="{Binding}" Name="myGrid1" IsSynchronizedWithCurrentItem="True" Margin="170,12,192,0" Height="272" VerticalAlignment="Top"> <DataGrid.Columns> <DataGridTextColumn Header="Module" Binding="{Binding Path=ModuleID}" Width="*" /> <DataGridTextColumn Header="Source" Binding="{Binding Path=Source}" Width="*" /> <DataGridTextColumn Header="User" Binding="{Binding Path=UserName}" Width="*" /> <DataGridTextColumn Header="Change Date" Binding="{Binding Path=DateChanged}" Width="2*" /> </DataGrid.Columns> </DataGrid> <Button Content="Clear Filter" Command="{Binding Path=(filter:DataGridExtensions.ClearFilterCommand), ElementName=myGrid1}" Height="26" HorizontalAlignment="Left" Margin="710,16,0,0" Name="Button1" VerticalAlignment="Top" Width="112" /> <TreeView Name="tv" Background="LightYellow" DataContext="{Binding Path=Items}" ItemsSource="{Binding ChangesXMLDataProvider}" Margin="35,290,489,12"> </TreeView> <TextBlock Height="273" HorizontalAlignment="Left" DataContext="{Binding Path=Items}" Margin="414,290,0,0" Name="TextBlock1" Text="{Binding /Changes}" VerticalAlignment="Top" Width="373" /> </Grid> </UserControl>
I want the datagrid to be tied to the CollectionView and the treeview below to be tied to the ChangesXMLDataProvider property of the currentitem in the collectionview. My datacontext for the view is the ViewModel so I tried to set the datacontext of the grid and the treeview and the textblock to properties of the current item.
My first problem is that when I change rows on my datagrid the current item does not change or if it does the textblock is not updating becuase the text that shows there does nto change. The second issue I am having is no data shows up in the treeview so I do not knwo if my binding is incorrect or it has an issue with my templates for showing the data.
Any help woudl be greatly appreciated.
Here is the model
Partial Public Class AuditChange #Region "Primitive Properties" Public Overridable Property Id As Integer Implements EFPoco.Core.IEntity.ID Public Overridable Property ModuleID As String Public Overridable Property Source As String Public Overridable Property UserName As String Public Overridable Property DateChanged As Date Public Overridable Property Changes As String #End Region End Class
Below is my ViewModel
<pre lang="x-vbnet">Imports System.Linq Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Imports System.Collections.Specialized Imports System.ComponentModel Imports CashReceipts.EFPoco.Core Imports System.Data.Objects Imports System.Xml ''' <summary> ''' Represents a container of CustomerViewModel objects ''' that has support for staying synchronized with the ''' CustomerRepository. This class also provides information ''' related to multiple selected customers. ''' </summary> Public Class AllAuditReportingChangesViewModel Inherits WorkspaceViewModel #Region "Fields" Private ReadOnly _auditChangesRepository As AuditChangeRepository Private ReadOnly _WorkUnit As UnitOfWorkAuditReporting Private _filterCommand As RelayCommand #End Region ' Fields #Region "Constructor" Public Sub New() _WorkUnit = New UnitOfWorkAuditReporting() _auditChangesRepository = New AuditChangeRepository(_WorkUnit) Me.CreateAllAuditChanges() End Sub Public Sub New(ByVal auditChangesRepository As AuditChangeRepository, ByVal wrkUnit As UnitOfWorkAuditReporting) If auditChangesRepository Is Nothing Then Throw New ArgumentNullException("auditChangesRepository") End If If wrkUnit Is Nothing Then Throw New ArgumentNullException("wrkUnit") End If MyBase.DisplayName = "Audit Reporting" _auditChangesRepository = auditChangesRepository _WorkUnit = wrkUnit ' Populate the AllCustomers collection with CustomerViewModels. Me.CreateAllAuditChanges() End Sub Private Sub CreateAllAuditChanges() 'Dim lstPayTypes As New List(Of PayTypeViewModel)(From p In _payTypeRepository.GetAll() Select New PayTypeViewModel(p, _payTypeRepository, _WorkUnit)) 'For Each cvm As PayTypeViewModel In lstPayTypes ' AddHandler cvm.PropertyChanged, AddressOf OnPayTypeViewModelPropertyChanged 'Next cvm 'Me.AllPayTypes = New ObservableCollection(Of PayTypeViewModel)(lstPayTypes) Me.AllAuditChanges = New ObservableCollection(Of AuditChange)(_auditChangesRepository.GetAll()) AddHandler Me.AllAuditChanges.CollectionChanged, AddressOf OnCollectionChanged End Sub #End Region ' Constructor #Region "Public Interface" Private privateAllAuditChanges As ObservableCollection(Of AuditChange) Public Property AllAuditChanges() As ObservableCollection(Of AuditChange) Get Return privateAllAuditChanges End Get Private Set(ByVal value As ObservableCollection(Of AuditChange)) privateAllAuditChanges = value End Set End Property Private _items As CollectionViewSource = Nothing Public ReadOnly Property Items As ICollectionView Get If Items Is Nothing Then _items = New CollectionViewSource() _items.Source = AllAuditChanges AddHandler _items.View.CurrentChanging, AddressOf CreateXMLProvider End If Return _items.View End Get End Property Private Sub CreateXMLProvider(ByVal sender As Object, ByVal e As System.ComponentModel.CurrentChangingEventArgs) Dim d As New XmlDocument() d.LoadXml(_items.View.CurrentItem.Changes) _ChangesXMLDataProvider.Document = d End Sub Private _ChangesXMLDataProvider As New XmlDataProvider Public Property ChangesXMLDataProvider() As XmlDataProvider Get Return _ChangesXMLDataProvider End Get Set(ByVal value As XmlDataProvider) _ChangesXMLDataProvider = value OnPropertyChanged("ChangesXMLDataProvider") End Set End Property #End Region ' Public Interface #Region " Base Class Overrides" Protected Overrides Sub OnDispose() Me.AllAuditChanges.Clear() RemoveHandler Me.AllAuditChanges.CollectionChanged, AddressOf OnCollectionChanged End Sub #End Region ' Base Class Overrides #Region "Event Handling Methods" Private Sub OnCollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs) If e.Action = NotifyCollectionChangedAction.Add Then For Each custVM As AuditChange In e.NewItems _auditChangesRepository.Add(custVM) Next ElseIf e.Action = NotifyCollectionChangedAction.Remove Then For Each custVM As AuditChange In e.OldItems _auditChangesRepository.Delete(custVM) Next End If End Sub #End Region ' Event Handling Methods End Class
Thursday, June 9, 2011 6:13 PM
Answers
-
Hi swade30,
1) If you want to set the same DataContext property to DataGrid, TreeView, TextBlock, I think there is no need:
DataContext="{Binding Path=Items}"
you could just set the DataContext property to the Root element, and then children element will share this DataContext, for example, you could set the DataContext to the Grid, like:
Grid.DataContext = Items.
2) Text="{Binding /Changes}" what is the "/Changes", if it is a property include in DataContext?
3) When you run application, you could check the "OutPut" window, and check if there is Binding exception, I guess your binding part will throw expcetion.
4) you could base on the exception to fix your issue, there is a way to debug your Binding, you could refer to,
http://social.msdn.microsoft.com/Forums/en-NZ/wpf/thread/549eeb7c-8df7-4a6c-a264-91f06ca75293
http://bea.stollnitz.com/blog/?p=52
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Sheldon _Xiao Thursday, June 16, 2011 10:44 AM
- Marked as answer by Sheldon _Xiao Tuesday, July 5, 2011 5:16 AM
Monday, June 13, 2011 6:03 AM -
OK I fixed the currentItem issue by changing my datacontext settings. I took the settings off the individual controls and put the datacontext on the parent grid and now the current item changes correctly and the controls refresh. Now my only issue is not being able to see data in the TreeView control. I coudl see the data change when I changed what its ItemSource was so it is bound correctly there is either an issue with my templates or a problem with the XMLDataProvider
- Marked as answer by Sheldon _Xiao Tuesday, July 5, 2011 5:16 AM
Wednesday, June 15, 2011 7:18 PM
All replies
-
Hi swade30,
1) If you want to set the same DataContext property to DataGrid, TreeView, TextBlock, I think there is no need:
DataContext="{Binding Path=Items}"
you could just set the DataContext property to the Root element, and then children element will share this DataContext, for example, you could set the DataContext to the Grid, like:
Grid.DataContext = Items.
2) Text="{Binding /Changes}" what is the "/Changes", if it is a property include in DataContext?
3) When you run application, you could check the "OutPut" window, and check if there is Binding exception, I guess your binding part will throw expcetion.
4) you could base on the exception to fix your issue, there is a way to debug your Binding, you could refer to,
http://social.msdn.microsoft.com/Forums/en-NZ/wpf/thread/549eeb7c-8df7-4a6c-a264-91f06ca75293
http://bea.stollnitz.com/blog/?p=52
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Sheldon _Xiao Thursday, June 16, 2011 10:44 AM
- Marked as answer by Sheldon _Xiao Tuesday, July 5, 2011 5:16 AM
Monday, June 13, 2011 6:03 AM -
Thanks for the reply.
1) If I change the line DataContext="{Binding Path=Items}" to DataContext=Items I no longer see the items in the grid.
2) From what I had read the / is supposed to tell the program I am looing at the current item of the collectionview that is my datacontext. Items is a collectionview so I wanted to get the Changes of the currently selected record in the grid. Changes is a property that holds an XML document containing all the information about changes made to the record.
3) I did have a few errors and I have tweaked the following code to resolve it.
Original
<TreeView Name="tv" Background="LightYellow" DataContext="{Binding Path=Items}" ItemsSource="{Binding ChangesXMLDataProvider}" Margin="35,290,489,12">
New
<TreeView Name="tv" Background="LightYellow" ItemsSource="{Binding ChangesXMLDataProvider}" Margin="35,290,489,12">
It was looking in the CollectionView for the ChangesXMLDataProvider but this property was actually in the ViewModel so I removed the DataContext setting allowing the TreeView to take the datacontext of the usercontrol which is the ViewModel. This resolved the only binding error I had but the treeview still shows no data in it.
So unfortuanately I still have the same issues. the TreeView does nto show any data and the textbox, wehich I just threw in for testing purposes, shows the XML for the first record but if I select another record in the grid it does not update to show the correct information.
Tuesday, June 14, 2011 8:48 PM -
OK I changed things a bit. I added a viewmodel for an individual AuditChange record and now in my AllAuditChangesViewModel I expose a CollectionView of the individual ViewModels with a property whic returns an XMLDataProvider. The new ViewModel is shown below
Imports System Imports System.ComponentModel Imports System.Windows.Input Imports CashReceipts.EFPoco.Core Imports System.Data.Objects Imports System.Xml ''' <summary> ''' A UI-friendly wrapper for a Customer object. ''' </summary> Public Class AuditReportingChangesViewModel Inherits WorkspaceViewModel Implements IDataErrorInfo #Region "Fields" Private ReadOnly _WorkUnit As UnitOfWorkAuditReporting Private ReadOnly _auditChange As AuditChange Private ReadOnly _auditChangeRepository As AuditChangeRepository Private _isSelected As Boolean Private _saveCommand As RelayCommand #End Region ' Fields #Region "Constructor" Public Sub New() _WorkUnit = New UnitOfWorkAuditReporting() _auditChangeRepository = New AuditChangeRepository(_WorkUnit) If _auditChangeRepository.Any Then _auditChange = _auditChangeRepository.First() Else _auditChange = AuditChange.CreateNew End If End Sub Public Sub New(ByVal auditChange As AuditChange, ByVal auditChangeRepository As AuditChangeRepository, ByVal wrkUnit As UnitOfWorkAuditReporting) If auditChange Is Nothing Then Throw New ArgumentNullException("auditChange") End If If auditChangeRepository Is Nothing Then Throw New ArgumentNullException("auditChangeRepository") End If If wrkUnit Is Nothing Then Throw New ArgumentNullException("wrkUnit") End If _auditChange = auditChange _auditChangeRepository = auditChangeRepository _WorkUnit = wrkUnit End Sub #End Region ' Constructor #Region "Customer Properties" Public Property ID As Integer Get Return _auditChange.Id End Get Set(ByVal value As Integer) If value = _auditChange.Id Then Return End If _auditChange.Id = value MyBase.OnPropertyChanged("ID") End Set End Property Public Property ModuleID() As String Get Return _auditChange.ModuleID End Get Set(ByVal value As String) If value = _auditChange.ModuleID Then Return End If _auditChange.ModuleID = value MyBase.OnPropertyChanged("ModuleID") End Set End Property Public Property Source() As String Get Return _auditChange.Source End Get Set(ByVal value As String) If value = _auditChange.Source Then Return End If _auditChange.Source = value MyBase.OnPropertyChanged("Source") End Set End Property Public Property UserName() As String Get Return _auditChange.UserName End Get Set(ByVal value As String) If value = _auditChange.UserName Then Return End If _auditChange.UserName = value MyBase.OnPropertyChanged("UserName") End Set End Property Public Property DateChanged() As DateTime Get Return _auditChange.DateChanged End Get Set(ByVal value As DateTime) If value = _auditChange.DateChanged Then Return End If _auditChange.DateChanged = value MyBase.OnPropertyChanged("DateChanged") End Set End Property Public Property Changes() As String Get Return _auditChange.Changes End Get Set(ByVal value As String) If value = _auditChange.Changes Then Return End If _auditChange.Changes = value MyBase.OnPropertyChanged("Changes") End Set End Property Private _ChangesXMLDataProvider As New XmlDataProvider Public ReadOnly Property ChangesXMLDataProvider() As XmlDataProvider Get Dim d As New XmlDocument() d.LoadXml(_auditChange.Changes) _ChangesXMLDataProvider.Document = d Return _ChangesXMLDataProvider End Get End Property #End Region ' Customer Properties #Region "Presentation Properties" Public Overrides Property DisplayName() As String Get Return "Audit Changes" End Get Protected Set(ByVal value As String) MyBase.DisplayName = value OnPropertyChanged("DisplayName") End Set End Property ''' <summary> ''' Returns a command that saves the customer. ''' </summary> Public ReadOnly Property SaveCommand() As ICommand Get If _saveCommand Is Nothing Then Dim saveAction As New Action(Of Object)(AddressOf Me.Save) _saveCommand = New RelayCommand(saveAction, Function(param) Me.CanSave) End If Return _saveCommand End Get End Property #End Region ' Presentation Properties #Region "Public Methods" ''' <summary> ''' Saves the customer to the repository. This method is invoked by the SaveCommand. ''' </summary> Public Sub Save() If (Not _auditChange.IsValid) Then Throw New InvalidOperationException("Unable to Save") End If If Me.IsNewPayType Then _auditChangeRepository.Add(_auditChange) End If _WorkUnit.Commit() MsgBox("Save Success!") End Sub #End Region ' Public Methods #Region "Private Helpers" ''' <summary> ''' Returns true if this customer was created by the user and it has not yet ''' been saved to the customer repository. ''' </summary> Private ReadOnly Property IsNewPayType() As Boolean Get Return _auditChange.Id <= 0 End Get End Property ''' <summary> ''' Returns true if the customer is valid and can be saved. ''' </summary> Private ReadOnly Property CanSave() As Boolean Get Return _auditChange.IsValid End Get End Property #End Region ' Private Helpers #Region "IDataErrorInfo Members" Private ReadOnly Property IDataErrorInfo_Error() As String Implements IDataErrorInfo.Error Get Return (TryCast(_auditChange, IDataErrorInfo)).Error End Get End Property Public ReadOnly Property IDataErrorInfo_Item(ByVal propertyName As String) As String Implements IDataErrorInfo.Item Get Dim [error] As String = Nothing [error] = (TryCast(_auditChange, IDataErrorInfo))(propertyName) ' Dirty the commands registered with CommandManager, ' such as our Save command, so that they are queried ' to see if they can execute now. CommandManager.InvalidateRequerySuggested() Return [error] End Get End Property #End Region ' IDataErrorInfo Members End Class
Below I show the new AllAuditChangesViewModel
Imports System.Linq Imports System Imports System.Collections.Generic Imports System.Collections.ObjectModel Imports System.Collections.Specialized Imports System.ComponentModel Imports CashReceipts.EFPoco.Core Imports System.Data.Objects Imports System.Xml ''' <summary> ''' Represents a container of CustomerViewModel objects ''' that has support for staying synchronized with the ''' CustomerRepository. This class also provides information ''' related to multiple selected customers. ''' </summary> Public Class AllAuditReportingChangesViewModel Inherits WorkspaceViewModel #Region "Fields" Private ReadOnly _auditChangesRepository As AuditChangeRepository Private ReadOnly _WorkUnit As UnitOfWorkAuditReporting Private _filterCommand As RelayCommand #End Region ' Fields #Region "Constructor" Public Sub New() _WorkUnit = New UnitOfWorkAuditReporting() _auditChangesRepository = New AuditChangeRepository(_WorkUnit) Me.CreateAllAuditChanges() End Sub Public Sub New(ByVal auditChangesRepository As AuditChangeRepository, ByVal wrkUnit As UnitOfWorkAuditReporting) If auditChangesRepository Is Nothing Then Throw New ArgumentNullException("auditChangesRepository") End If If wrkUnit Is Nothing Then Throw New ArgumentNullException("wrkUnit") End If MyBase.DisplayName = "Audit Reporting" _auditChangesRepository = auditChangesRepository _WorkUnit = wrkUnit ' Populate the AllCustomers collection with CustomerViewModels. Me.CreateAllAuditChanges() End Sub Private Sub CreateAllAuditChanges() Dim all As List(Of AuditReportingChangesViewModel) = ( _ From cust In _auditChangesRepository.GetAll() _ Select New AuditReportingChangesViewModel(cust, _auditChangesRepository, _WorkUnit)).ToList() For Each cvm As AuditReportingChangesViewModel In all AddHandler cvm.PropertyChanged, AddressOf OnEmployeeViewModelPropertyChanged Next cvm Me.AllAuditChanges = New ObservableCollection(Of AuditReportingChangesViewModel)(all) AddHandler Me.AllAuditChanges.CollectionChanged, AddressOf OnCollectionChanged End Sub #End Region ' Constructor #Region "Public Interface" Private privateAllAuditChanges As ObservableCollection(Of AuditReportingChangesViewModel) Public Property AllAuditChanges() As ObservableCollection(Of AuditReportingChangesViewModel) Get Return privateAllAuditChanges End Get Private Set(ByVal value As ObservableCollection(Of AuditReportingChangesViewModel)) privateAllAuditChanges = value End Set End Property Private _items As CollectionViewSource = Nothing Public ReadOnly Property Items As ICollectionView Get If Items Is Nothing Then _items = New CollectionViewSource() _items.Source = AllAuditChanges End If Return _items.View End Get End Property #End Region ' Public Interface #Region " Base Class Overrides" Protected Overrides Sub OnDispose() Me.AllAuditChanges.Clear() RemoveHandler Me.AllAuditChanges.CollectionChanged, AddressOf OnCollectionChanged End Sub #End Region ' Base Class Overrides #Region "Event Handling Methods" Private Sub OnCollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs) If e.NewItems IsNot Nothing AndAlso e.NewItems.Count <> 0 Then For Each empVM As AuditReportingChangesViewModel In e.NewItems AddHandler empVM.PropertyChanged, AddressOf OnEmployeeViewModelPropertyChanged Next empVM End If If e.OldItems IsNot Nothing AndAlso e.OldItems.Count <> 0 Then For Each empVM As AuditReportingChangesViewModel In e.OldItems RemoveHandler empVM.PropertyChanged, AddressOf OnEmployeeViewModelPropertyChanged Next empVM End If End Sub Private Sub OnEmployeeViewModelPropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Dim IsSelected As String = "IsSelected" ' Make sure that the property name we're referencing is valid. ' This is a debugging technique, and does not execute in a Release build. TryCast(sender, AuditReportingChangesViewModel).VerifyPropertyName(IsSelected) ' When a customer is selected or unselected, we must let the ' world know that the TotalSelectedSales property has changed, ' so that it will be queried again for a new value. If e.PropertyName = IsSelected Then Me.OnPropertyChanged("TotalAnnualSalary") End If End Sub Private Sub OnEmployeeAddedToRepository(ByVal sender As Object, ByVal e As AuditChangeAddedEventArgs) Dim viewModel = New AuditReportingChangesViewModel(e.NewAuditChange, _auditChangesRepository, _WorkUnit) Me.AllAuditChanges.Add(viewModel) End Sub #End Region ' Event Handling Methods End Class
Below is the changes I made to xaml on the screen to try to show info on the TreeView cointrol
<UserControl x:Class="AuditReportingView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="562" d:DesignWidth="880" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" xmlns:filter="clr-namespace:DataGridFilterLibrary;assembly=DataGridFilterLibrary"> <!--<UserControl.Resources> <DataTemplate DataType="ChangeNode"> <StackPanel Margin="6" Orientation="Horizontal" > <TextBlock Text="{Binding XPath=@Type}" /> <TextBlock Text="{Binding XPath=@ChangeType}" /> <TextBlock Text="{Binding XPath=@Description}" /> </StackPanel> </DataTemplate> <DataTemplate DataType="ChangeDatailNode"> <StackPanel Margin="6" Orientation="Horizontal" > <TextBlock Text="{Binding XPath=@Description}" /> <TextBlock Text="{Binding XPath=@OldValue}" /> <TextBlock Text="{Binding XPath=@NewValue}" /> </StackPanel> </DataTemplate> <HierarchicalDataTemplate DataType="ChangeDetails" ItemsSource="{Binding XPath=*}"> <TextBlock Text="Change Information"/> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="Children" ItemsSource="{Binding XPath=*}"> <TextBlock Text="Details"/> </HierarchicalDataTemplate> </UserControl.Resources>--> <Grid Width="881" Height="559"> <DataGrid filter:DataGridExtensions.UseBackgroundWorkerForFiltering="True" filter:DataGridExtensions.IsClearButtonVisible="false" filter:DataGridExtensions.IsFilterVisible="True" ColumnHeaderStyle="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type filter:DataGridHeaderFilterControl}, ResourceId=DataGridHeaderFilterControlStyle}}" AutoGenerateColumns="False" DataContext="{Binding Path=Items}" ItemsSource="{Binding}" Name="myGrid1" IsSynchronizedWithCurrentItem="True" Margin="170,12,192,0" Height="272" VerticalAlignment="Top"> <DataGrid.Columns> <DataGridTextColumn Header="Module" Binding="{Binding Path=ModuleID}" Width="*" /> <DataGridTextColumn Header="Source" Binding="{Binding Path=Source}" Width="*" /> <DataGridTextColumn Header="User" Binding="{Binding Path=UserName}" Width="*" /> <DataGridTextColumn Header="Change Date" Binding="{Binding Path=DateChanged}" Width="2*" /> </DataGrid.Columns> </DataGrid> <Button Content="Clear Filter" Command="{Binding Path=(filter:DataGridExtensions.ClearFilterCommand), ElementName=myGrid1}" Height="26" HorizontalAlignment="Left" Margin="710,16,0,0" Name="Button1" VerticalAlignment="Top" Width="112" /> <TreeView Name="tv" Background="LightYellow" DataContext ="{Binding Path=Items}" ItemsSource="{Binding /Changes}" Margin="35,290,489,12"> </TreeView> <TextBlock Height="273" HorizontalAlignment="Left" DataContext="{Binding Path=Items}" Margin="414,290,0,0" Name="TextBlock1" Text="{Binding /Changes}" VerticalAlignment="Top" Width="373" /> </Grid> </UserControl>
For testing purposes I changed the databinding of the TreeView to be the Changes property and I commented otu my data templates. The treeview then showed me data. This means to me that either the binding to the xmldataprovider is incorrect or the datatemplates are incorrect and 1 of these things is causing me issues with showing the data.
I am still having the issue where if I move from item to item in the grid I can see my current item of the collectionview changes but the controls tied to properties of the current item do not seem to refresh.
Any help would be greatly appreciated.
Wednesday, June 15, 2011 2:03 PM -
OK I fixed the currentItem issue by changing my datacontext settings. I took the settings off the individual controls and put the datacontext on the parent grid and now the current item changes correctly and the controls refresh. Now my only issue is not being able to see data in the TreeView control. I coudl see the data change when I changed what its ItemSource was so it is bound correctly there is either an issue with my templates or a problem with the XMLDataProvider
- Marked as answer by Sheldon _Xiao Tuesday, July 5, 2011 5:16 AM
Wednesday, June 15, 2011 7:18 PM -
Hi swad30,
How about your issue, if your issue persists, could you tell me your main concern now?
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Saturday, June 18, 2011 4:31 AM