如何解决C#中的简单依赖项属性和UserControl问题
|| 我的最终目标是从XAML中UserControl
的调用中暴露I2ѭ中的TextBox
的the0ѭ值。
<my:UserControl SetCustomText=\"Blah blah this is variable\">
会用TextBox
的文本呈现UserControl
。
我一直在使用各种示例进行处理,但是我总是以\“在UserControl类型中找不到属性SetCustomText \”结束
解决方法
如何执行此操作的示例:
<UserControl x:Class=\"Test.UserControls.MyUserControl1\"
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"
xmlns:d=\"http://schemas.microsoft.com/expression/blend/2008\"
Name=\"control\">
<Grid>
<!-- Text is being bound to outward representative property -->
<TextBox Text=\"{Binding MyTextProperty,ElementName=control}\"/>
</Grid>
</UserControl>
public partial class MyUserControl1 : UserControl
{
// The dependency property which will be accessible on the UserControl
public static readonly DependencyProperty MyTextPropertyProperty =
DependencyProperty.Register(\"MyTextProperty\",typeof(string),typeof(MyUserControl1),new UIPropertyMetadata(String.Empty));
public string MyTextProperty
{
get { return (string)GetValue(MyTextPropertyProperty); }
set { SetValue(MyTextPropertyProperty,value); }
}
public MyUserControl1()
{
InitializeComponent();
}
}
<uc:MyUserControl1 MyTextProperty=\"Text goes here\"/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。