C# 是否支持Silverlight控件中的文本内容?
我的silverlight控件可以支持将内容传递到其中,如下所示:C# 是否支持Silverlight控件中的文本内容?,c#,silverlight,xaml,text,C#,Silverlight,Xaml,Text,我的silverlight控件可以支持将内容传递到其中,如下所示: <MyControl> <OtherControl/> </MyControl> 但如果我这样做: <MyControl> THIS IS TEXT </MyControl> 这是文本 如果尝试运行它,我会收到一个错误消息: MyControl不支持文本内容。[第142行位置: 72] MyControl的item属性应该支持作为对象的所有内容,因此它可以支
<MyControl>
<OtherControl/>
</MyControl>
但如果我这样做:
<MyControl>
THIS IS TEXT
</MyControl>
这是文本
如果尝试运行它,我会收到一个错误消息:
MyControl不支持文本内容。[第142行位置:
72]
MyControl的item属性应该支持作为对象的所有内容,因此它可以支持Textbox、按钮和其他控件。但是,如果我试图只传递原始文本,它就不起作用
我知道这应该是可行的,我唯一的问题是,如何做?您需要告诉编译器类的什么属性将用于XAML中标记之间的内容 假设您要使用的属性是
public string Title { get; set; }
在类定义上方,需要添加ContentProperty属性,如下所示:
[ContentProperty("Title")]
public class MyControl
{
// class code
}
您需要告诉编译器类的什么属性将用于XAML中标记之间的内容 假设您要使用的属性是
public string Title { get; set; }
在类定义上方,需要添加ContentProperty属性,如下所示:
[ContentProperty("Title")]
public class MyControl
{
// class code
}
您必须从以下方面获得控制权:
public class SimpleControl : ContentControl {
}
一些文字。。。
您必须从以下内容派生控件:
public class SimpleControl : ContentControl {
}
一些文字。。。
您必须为控件定义一个依赖项属性,您可以从控件的绑定中使用该属性。
参见FLOWING代码:
public class customtextbox : UserControl
{
public static readonly DependencyProperty TextProperty =
TextBox.TextProperty.AddOwner(typeof(customtextbox));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
然后设置文本属性:
<CT:customtextbox Text="somthing"/> OR
<CT:customtextbox Text="{Binding mypropertyinviewmodel}"/>
或
您必须为控件定义一个依赖项属性,您可以从控件的绑定中使用该属性。
参见FLOWING代码:
public class customtextbox : UserControl
{
public static readonly DependencyProperty TextProperty =
TextBox.TextProperty.AddOwner(typeof(customtextbox));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
}
然后设置文本属性:
<CT:customtextbox Text="somthing"/> OR
<CT:customtextbox Text="{Binding mypropertyinviewmodel}"/>
或
我完全不知道,但如果它包含字符串,则类似于OnApplyTemplate中的外观。如果它确实将字符串包装在文本框中,并将其添加到控件中。我不知道确切的情况,但它与OnApplyTemplate中的外观类似,如果它包含字符串。如果它确实将字符串包装在文本框中并将其添加到控件中。我已经有一个名为Item的对象的ContProperty,并且不能多次添加它:-/I已经有一个名为Item的对象的ContProperty,并且不能多次添加它:-/