gpt4 book ai didi

WPF DependencyProperty 链通知

转载 作者:行者123 更新时间:2023-12-04 22:29:45 38 4
gpt4 key购买 nike

我正在探索 WPF 世界,我在网上找到了一个关于如何在 xml 上使用绑定(bind)的好例子

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

现在我试图扩展这个例子:我想在 XElement 之间创建一个“中间类”和 UI 并在一个链中绑定(bind)所有 togheder 所以,如果我对 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;
}

Book 是一个代表一本书的类,假设它现在只有一个名称属性。

UI 类未命中,但应绑定(bind)到公共(public) List<Book> BookList 并在 ListBox 中向用户显示书籍名称

Enyone 知道为什么我没有收到任何通知......或者我必须做些什么来保持公众 List<Book> BookList 与私有(private) IEnumerable<XBookList> 同步?

最佳答案

好的,经过多次尝试,我找到的唯一解决方案是这个:

IEnumerable<XBookList> 中发生变化时收到通知您需要在修改后清除它并重新绑定(bind)。

通过这种方式,您有一个关于清除的第一个未使用的通知,然后是关于新集合的另一个通知。

然后在处理程序中,您可以将新列表与旧列表同步。

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

关于WPF DependencyProperty 链通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12955114/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com