我有一个自定义面板:
public class DevicesPanel : Canvas { ... }
像这样使用:
<vc:DevicesPanel>
...
</vc:DevicesPanel>
如何添加XAML
Grid.RowDefinitions
小组的Grid
属性?它可以像这样使用:
<vc:DevicesPanel>
<vc:DevicesPanel.Data/>
...
</vc:DevicesPanel>
也是这样的:
<vc:DevicesPanel>
<vc:DevicesPanel.Data>
...
</vc:DevicesPanel.Data>
...
</vc:DevicesPanel>
我试过了:
public class DevicesPanel : Canvas {
public static readonly DependencyProperty XyProperty =
DependencyProperty.Register("Xy",
typeof (IEnumerable<UIElement>),
typeof (DevicesPanel),
new PropertyMetadata(default(IEnumerable<UIElement>)));
public IEnumerable<UIElement> Xy {
get { return (Collection<UIElement>)GetValue(XyProperty); }
set { SetValue(XyProperty, value); }
}
...
}
但是这不能编译(错误发生在XAML
部分):
<vc:DevicesPanel>
<vc:DevicesPanel.Xy></vc:DevicesPanel.Xy>
...
<vc:DevicesPanel>
错误是:
Property 'Xy' does not have a value.
The attachable property 'Xy' was not found in type 'DevicesPanel'.
The member "Xy" is not recognized or is not accessible.
(也尝试Collection
代替IEnumerable
)
答案 0 :(得分:1)
由于Grid.RowDefinitions
只是一个公共属性,您可以将此属性添加到DevicesPanel
类。
// DevicesPanel.cs
public class DevicesPanel : Canvas
{
public List<string> Data { get; set; }
}
// MainWindow.xaml
<wpfApplication1:DevicesPanel>
<wpfApplication1:DevicesPanel.Data>
<system:String>Item1</system:String>
<system:String>Item2</system:String>
<system:String>Item3</system:String>
</wpfApplication1:DevicesPanel.Data>
</wpfApplication1:DevicesPanel>