Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF C#4.0
I would like to bind a ViewModell with an xml to my view, like this one:
 
 class ViewModel : INotifyPropertyChanged 
        {
            public XmlDataProvider Dataprov { get; set; }
 
            
            private string _CommonProperty;
 
            public string CommonProperty
            {
                get { return _CommonProperty; }
                set { _CommonProperty = value; OnPropertyChanged(); }
            }
 
            ...
        }
 
I added the viewmodel to datacontext in this way:
 
public MainWindow()
        {
            InitializeComponent();
            vm = new ViewModel();
            vm.Dataprov = new XmlDataProvider();
            try
            {
                vm.Dataprov.Source = new Uri("Test.xml",UriKind.Relative);
            }
            catch (Exception ex)
            {}
            this.DataContext = vm;
        }
 
Then I tried multiple ways to get xml data in my view, but every tries failed Frown | :(
 
for example:
 <Grid>
        
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding  Source=Dataprov, XPath='Root/test1/test11'}" ></Label>
        </StackPanel>
</Grid>
 
 
Is there any possible way to use xml binding, if the XmlDataProvider is part of the ViewModel?
 
Thank you
Posted 11-Jan-13 3:47am
CzimerA1.9K
Comments
Suvabrata Roy at 11-Jan-13 8:55am
   
Are you sure about your xml file format ?
If so then share the xml.
 
Other wise its very tough to understand.
czimera at 11-Jan-13 9:30am
   
Im sure, If the datacontext is the XMLDataProvider the binding works.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
For your reference :
Link[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I have found a walkaround solution for this issue. The XmlDataProvider was removed from the viewmodel,and was given to the resources:
<window.resources>
   <XmlDataProvider x:Key="Lang" Source="Test.xml" XPath="Root/test1"  />
</window.resources>
Lets say the viewmodel like this:
class ViewModel : INotifyPropertyChanged 
{
  private bool _PropEnabled;
 
  public bool PropEnabled
  {
    get { return _PropEnabled; }
    set { _PropEnabled = value; OnPropertyChanged(); }
    ...
  }
}
Now the binding could be accomplished:
<Label Content="{Binding Source={StaticResource ResourceKey=Lang}, XPath='test11'}" IsEnabled="{Binding PropEnabled}" ></Label>
So the XML and the view can be use for binding in the same time. The Content is binded to the XML, but the IsEnabled property is binded to the viewmodel.
I have to highlight this is only a workaround for my problem, but how to merge multiple viewmodels is still unclean for me.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100