Click here to Skip to main content
13,190,405 members (52,977 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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()
            vm = new ViewModel();
            vm.Dataprov = new XmlDataProvider();
                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 :(

for example:
        <StackPanel Orientation="Vertical">
            <Label Content="{Binding  Source=Dataprov, XPath='Root/test1/test11'}" ></Label>

Is there any possible way to use xml binding, if the XmlDataProvider is part of the ViewModel?

Thank you
Posted 11-Jan-13 2:47am
Suvabrata Roy 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 11-Jan-13 9:30am
Im sure, If the datacontext is the XMLDataProvider the binding works.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


For your reference :
Rate this: bad
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:
   <XmlDataProvider x:Key="Lang" Source="Test.xml" XPath="Root/test1"  />

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171016.2 | Last Updated 14 Jan 2013
Copyright © CodeProject, 1999-2017
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