4

IM探索WPF的世界裏,我找到了如何使用綁定在XMLWPF的DependencyProperty鏈通知

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

現在,我嘗試在網絡上很好的例子,擴展了這個例子:我想創建一個「中間類「XElement和用戶界面之間,並綁定所有鏈接,所以,如果我有一個修改到XML中,那麼我有更新中間類的屬性,然後UI也更新了。

這裏是一些代碼:

這是包裹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; 
     } 

    } 
} 

這是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; 
    } 

書類是一類塔爾repersents一本書,讓說它現在只有一個名字。

的UI類未命中,但它應該在ListBox

Enyone公共List<Book>書目,並顯示書籍的名稱綁定到用戶知道這是爲什麼我不recive任何通知......或者我有什麼做的,以保持公開List<Book> BookList與私人同步IEnumerable<XBookList>

回答

0

OK,多次嘗試之後,我發現唯一的解決辦法是這樣的一個:

有通知時在IEnumerable<XBookList>一些變化,你需要清除它的廣告,當你修改它重新綁定。

通過這種方式,您可以獲得第一個未使用的通知,其中包含有關新設置的清除通知和其他通知。

然後在處理程序中,您可以將新列表與舊列表同步。

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; 
} 
相關問題