Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:35:42+00:00 2026-05-28T01:35:42+00:00

Made a simple test project where i try to bind to a xmldatasource in

  • 0

Made a simple test project where i try to bind to a xmldatasource in a proto viewmodel

public partial class Window1 : Window
{
    //private XmlDataProvider _provider = new XmlDataProvider(); 
    private MyViewModel _myViewModel = new MyViewModel();
    public Window1()
    {
        InitializeComponent();
        this.DataContext = _myViewModel ;
    }    
}

public class MyViewModel
{    
    public MyViewModel()
    {
        LoadXMLData();
    }

    private XmlDataProvider _provider = new XmlDataProvider(); 
    public XmlDataProvider Reports
    {
        get { return _provider; }
        set { _provider = value; }
    }    

    private void LoadXMLData()
    {
        string filePath = Directory.GetCurrentDirectory() + @"\Reports2.xml";

        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(filePath);
        _provider.Document = doc;
        _provider.XPath = @"Reports/Report";
    }
}

If i try to bind a listbox like this. I get nothing

<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
    ItemsSource="{Binding Reports}"
    ItemTemplate="{StaticResource teamItemTemplate}"
    IsSynchronizedWithCurrentItem="True"
    Visibility="Visible" SelectionMode="Single">
</ListBox>

If i instead change datacontext to

this.DataContext = _myViewModel.Reports

And listbox to

<ListBox x:Name="TeamsListBox" Margin="0,0,0,20" DockPanel.Dock="Left"
    ItemsSource="{Binding}"
    ItemTemplate="{StaticResource teamItemTemplate}"
    IsSynchronizedWithCurrentItem="True"
    Visibility="Visible" SelectionMode="Single">
</ListBox>

Then it works, how do i bind to the viewmodel so i can fill it with more than just on xmldatasource

If i put a breakpoint on property Report i can see that it is called when i do {Binding Reports} but the list is still empty.

UPDATE

I can do this binding in code and then it works

 Binding binding = new Binding();
            binding.Source = _myViewModel.Reports;
            binding.XPath = @"Reports/Report";
            TeamsListBox.SetBinding(ListBox.ItemsSourceProperty, binding);

Why cant i do that in XAML

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:35:43+00:00Added an answer on May 28, 2026 at 1:35 am

    Seems like i had some problems with the understanding of XPath and my general question was how to bind to a dynamic xmldataprovider in a viewmodel with xaml. Solved it like this.

    XML

    <?xml version="1.0" encoding="utf-8"?>
    <Reports xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Report Id="AAAAA-ABBB">
        <DocId>30110001</DocId>
        <DocName>Report name1</DocName>
        <DocType>2010-01-01</DocType>
        <Status>1</Status>
        <CreatedById>1</CreatedById>
        <SiteId>1</SiteId>
        <Language>1</Language>
        <Updated>2011-01-01</Updated>
        <Published>2011-01-01</Published>
        <FilePath>c:\\reports\20011001.docx</FilePath>
      </Report>
      <Report Id="AAAAA-ABBC">
        <DocId>30110002</DocId>
        <DocName>Report name2</DocName>
        <DocType>2010-01-01</DocType>
        <Status>1</Status>
        <CreatedById>1</CreatedById>
        <SiteId>1</SiteId>
        <Language>1</Language>
        <Updated>2011-01-01</Updated>
        <Published>2011-01-01</Published>
        <FilePath>c:\\reports\20011001.docx</FilePath>
      </Report>
    </Reports>
    

    Window1

        <Window x:Class="WpfApplication2.Window1"
                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" 
                  Title="Window1" Height="300" Width="300">
            <Window.Resources>
    
                <DataTemplate x:Key="reportItemTemplate">
                    <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding XPath=DocId}"/>
                        <Label Content="{Binding XPath=DocName}"/>
                    </StackPanel>
                </DataTemplate>
            </Window.Resources>
            <StackPanel  DataContext="{Binding LocalReports}" >
                <ListBox  
    
                ItemsSource="{Binding}"
                    ItemTemplate="{StaticResource reportItemTemplate}"
                             IsSynchronizedWithCurrentItem="True"
                             Visibility="Visible" SelectionMode="Single"
                    />
                <TextBox Text="{Binding XPath=DocId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <TextBox Text="{Binding XPath=DocName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                <Button Content="Button" Height="23" Name="button1" Click="button1_Click" />
            </StackPanel>
    
        </Window> 
    

    Window1.xaml.cs

    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            //private XmlDataProvider _provider = new XmlDataProvider(); 
            private MyViewModel _myViewModel = new MyViewModel();
    
            public Window1()
            {
                InitializeComponent();
    
                this.DataContext = _myViewModel;
    
    
            }
    
    
             private void button1_Click(object sender, RoutedEventArgs e)
             {
                 _myViewModel.Save();
             }
    
        }
    
        public class MyViewModel
        {
    
            public MyViewModel()
            {
                          }
    
            private XmlDataProvider _provider;
            public XmlDataProvider LocalReports
            {
    
                get
                {
                    String file = Directory.GetCurrentDirectory() + @"\Reports2.xml";
                    _provider = new XmlDataProvider()
                    {
                        Source = new Uri(file, UriKind.Absolute),
                        XPath = "Reports/Report"
                    };
                    return _provider;
                }
            }
    
    
    
            }
    
    
            public  void Save()
            {
                string source = _provider.Source.LocalPath;
                _provider.Document.Save(source);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple test application for the issue, two winforms each containing
I'm putting together a simple test made up of two tutorials available online for
I have a little trouble getting gettext to work. I made a simple test
The project I'm working on is slightly more complicated but I made this simple
I've got a simple winform test app i'm using to try some Log4Net Dependency
I've made a simple app and I wanted to test pages for 404, 500
I have compiled a simple ASP.NET project to test first data global gateway API
I've made vb console application project in vs 2010 and added one class with
I have made a simple test applet that has a red background and a
On Windows, testing different OSes is made simple using VMs. Is there a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.