Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I would like to bind a ViewModell with an xml to my view, like this one:

C#
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:

C#
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 :(

for example:
XML
 <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
Comments
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.

Hi,

For your reference :
Link[^]
 
Share this answer
 
I have found a walkaround solution for this issue. The XmlDataProvider was removed from the viewmodel,and was given to the resources:
XML
<window.resources>
   <XmlDataProvider x:Key="Lang" Source="Test.xml" XPath="Root/test1"  />
</window.resources>

Lets say the viewmodel like this:
C#
class ViewModel : INotifyPropertyChanged 
{
  private bool _PropEnabled;

  public bool PropEnabled
  {
    get { return _PropEnabled; }
    set { _PropEnabled = value; OnPropertyChanged(); }
    ...
  }
}

Now the binding could be accomplished:
XML
<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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900