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 8720199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:58:49+00:00 2026-06-13T06:58:49+00:00

im exploring WPF world, i find a great example on the web about how

  • 0

im exploring WPF world, i find a great example on the web about how to use binding on xml

http://www.codeproject.com/Articles/37854/How-to-Perform-WPF-Data-Binding-Using-LINQ-to-XML

Now im trying to extends this example: i want to create a “class in the middle” between the XElement and the UI and bind all togheder in a chain so, if i have a modification into the xml, then i have the property in the middle class updated then the UI updated too.

Here some code:

This is the class that wrap the XElement

public class XElementDataProvider : ObjectDataProvider
{
    public XElementDataProvider()
    {
        ObjectInstance = XElement.Load(@"C:\MyFile.xml");
    }

    private static XElementDataProvider instance;


    public static XElementDataProvider Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new XElementDataProvider();
            }
            return instance;
        }

    }
}

This is the MiddleClass

public class MiddleClass : DependencyObject
{


    XElementDataProvider xElementDataProvider;
    XElement myxml;

    public MiddleClass()
    {
                    //here i get my dataprovider
        xElementDataProvider = XElementDataProvider.Instance;
        myxml = xElementDataProvider.Data as XElement;

                    //i bind my internal collection to the Elements...
        Binding binding = new Binding("Elements[book]")
        {
            Source = myxml,
            Mode = BindingMode.Default//here i cant use TwoWay, give me          //back an exeption
        };


        BindingOperations.SetBinding(this, XBookListProperty, binding);

                    //just to have confirmation of the adding
        myxml.Changed += new EventHandler<XObjectChangeEventArgs (myxml_Changed);

    }

    void myxml_Changed(object sender, XObjectChangeEventArgs e)
    {

    }

            //i use a DependencyProperty to have also a change callback
    public static readonly DependencyProperty XBookListProperty =
        DependencyProperty.Register("XBookList", typeof(IEnumerable),
        typeof(MiddleClass),
        new PropertyMetadata(XBookPropertyChanged)
        );


            //here i have a notification only at start but no when i add a new book
    private static void XBookPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MiddleClass middleClass = d as MiddleClass;
        middleClass.XBookPropertyChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue);
    }

    private void XBookPropertyChanged(IEnumerable old, IEnumerable newValue)
    {

    }
             //this is the propery i finally want to expose to the UI but im not able //to keep updated
    public List<Book> bookList;
    public List<Book> BookList
    {
        get
        {
            return bookList;
        }
        set
        {
            bookList = value;
        }
    }



    //this is my internal list binded to the xml
    private IEnumerable XBookList
    {
        get
        {
            return (IEnumerable)GetValue(XBookListProperty);
        }
        set
        {
            SetValue(XBookListProperty, value);
        }
    }

            //here i try to add a book addind direcly to the xml//i expect a //notification of propery changed...but nothing
    public bool AddBook(string name)
    {

        XElement newWorkSheet = new XElement("Book",
            new XAttribute("Name", name)
            );

        myxml.Add(newWorkSheet);


        return true;
    }

Book is a class thar repersents a book, let say it has only a name propery for now.

The UI class misses but it should bind on public List<Book> BookList and show books names to the user in a ListBox

Enyone knows why i dont recive any notification…or what i have to do to keep the public List<Book> BookList synchronized with private IEnumerable<XBookList>?

  • 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-06-13T06:58:52+00:00Added an answer on June 13, 2026 at 6:58 am

    OK, after many attempts, the only solution I found is this one:

    to have notifications when something changes in the IEnumerable<XBookList> you need to clear it ad rebind after you modify it.

    In this way you have a first, not used notification, about the clear and then another notification about the new set.

    Then in the handler you can synchronize the new list with the old one.

    public bool AddBook(string name)
    {
    
        XElement newWorkSheet = new XElement("Book",
            new XAttribute("Name", name)
            );
    
        myxml.Add(newWorkSheet);
    
    
        ClearValue(XBookListProperty);
    Binding binding = new Binding("Elements[book]")
        {
            Source = myxml,
            Mode = BindingMode.Default          
        };
        BindingOperations.SetBinding(this, XBookListProperty, binding);
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently exploring a bug in the template used on http://www.ecovivo.be/rubriek/food . Problem:
A beginner in WPF Documents here. I am exploring the possibility to display page
I have been exploring different answers about release management in Mercurial and almost found
I'm currently exploring the different options for building a not-too-complex web application, in which
I've been exploring WPF and XAML for a while now, but have hit a
After exploring the very excellent answer from Heike to my previous question about anamorphic
Exploring Google's +1 Button, I found two things odd about the code they supply:
I'm exploring query notifications with the SQLDependency class. Building a simple working example is
I'm working on my first C#/WPF project (I'm a Java/Web developer with some Flex/As
Recently I came to know from this article - http://blogs.msdn.com/b/patrickdanino/archive/2009/11/11/custom-controls-and-ui-automation.aspx -that controls in WPF

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.