Fre*_*lad 11 c# silverlight wpf xaml windows-phone-7
考虑以下Xaml
<Grid>
<TextBox>Text</TextBox>
<Button>Content</Button>
</Grid>
Run Code Online (Sandbox Code Playgroud)
它会设置
但这是如何规定的?如何指定Xaml中开始和结束标记之间的哪个属性?
这是由依赖属性中的某些元数据设置还是什么?
谢谢
Abe*_*cht 17
有一个ContentPropertyAttribute适用于一个类.WPF/Silverlight将使用反射来确定要使用的属性.
如果您想使用自定义类执行此操作,您可以这样做:
[ContentProperty("Bar")]
public class Foo : Control
{
public static DependencyProperty BarProperty = DependencyProperty.Register(
"Bar",
typeof(int),
typeof(Foo),
new FrameworkPropertyMetaData(0));
public int Bar
{
get { return (int)GetValue(BarProperty); }
set { SetValue(BarProperty, value); }
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样在XAML中指定它:
<lcl:Foo>12</lcl:Foo>
Run Code Online (Sandbox Code Playgroud)
更新
由于它使用反射,因此您不需要执行DependencyProperty.例如,这也将起作用:
[ContentProperty("Bar")]
public class Foo : Control
{
public int Bar { get; set; }
}
Run Code Online (Sandbox Code Playgroud)