4
는 WPF의 세계를 탐험, 내가

는 WPF DependencyProperty에 체인 알림

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

지금에 노력 메신저 XML에 바인딩을 사용하는 방법에 대한 웹에 좋은 예를 찾을 수

메신저이 예제를 확장 : 난 "은을 만들려면 클래스 중간에 "XElement 사이의 UI 및 사슬에있는 모든 togheder 바인딩, 만약 내가 XML에 수정하면 중간 클래스의 속성을 누른 다음 UI를 업데이 트했습니다. 여기

일부 코드 :

이이이 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; 
    } 

책이다 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; 
     } 

    } 
} 

포장 클래스가 클래스 타르 책을 repersents입니다, 그것을 가정 해 봅시다 지금은 단지 이름의 성질을 가지고있다.

하는 UI 클래스 미스하지만이 ListBox

Enyone에서 사용자에게 List<Book> 공공 BookList와 쇼 책 이름에 바인드해야 내가 어떤 통지를 받아 봐 해달라고 ... 또는 내가 가지고있는 것은 계속해야 할 이유를 알고 공개 List<Book> 북 목록은 개인과 동기화 IEnumerable<XBookList>?

답변

0

확인, 많은 시도 후, 내가 찾은 유일한 해결책이 하나 다음 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; 
}