我想有一個DependencyPropery,它是一個引用的UIElements數組。在WPF中,我們有x:數組標記擴展,還有x:Reference可用,但它無法使其工作。數組引用
有關於此的任何想法?
克里斯
我想有一個DependencyPropery,它是一個引用的UIElements數組。在WPF中,我們有x:數組標記擴展,還有x:Reference可用,但它無法使其工作。數組引用
有關於此的任何想法?
克里斯
標準的方式做,這是WPF是對UIElementCollection類分配給您的依賴屬性,如:
public UIElementCollection ElementCollection
{
get { return (UIElementCollection)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropertyProperty =
DependencyProperty.Register("MyProperty", typeof(UIElementCollection), typeof(MyClass), new UIPropertyMetadata(null));
在類的構造函數,你應該設置依賴項屬性爲new UIElementCollection
。
剛剛找到答案我自己。
MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(UIElement[]), typeof(MyClass), new FrameworkPropertyMetadata(null, MyPropertyChanged));
以及相應的XAML看起來像:
<cc:MyClass.MyProperty>
<x:Array Type="UIElement">
<x:Reference>NameOfTheReferencedUIElement</x:Reference>
</x:Array>
</cc:MyClass.MyProperty>
這種方式依賴屬性自動填充引用UI對象在UI元素的形式的陣列。
Chris
您確定這適合於WPF嗎?根據http://msdn.microsoft.com/en-us/library/ee792007.aspx,x:引用不能用於WPF。 – 2012-03-21 12:07:44
是的!請參閱WPF 4.0中的http://msdn.microsoft.com/en-us/library/ee795380.aspx – balistof 2012-03-21 13:18:19
@fmunkert:'x:Reference'是一個例外,文章也沒有說明它不能被使用,它只是聲明它在編譯XAML中不起作用。 – 2012-03-21 17:22:09
感謝您對UIElementCollection的提示! – balistof 2012-03-21 11:32:34