[英]Simple Dependency Property and UserControl issues in C#
My end goal is to expose the Text
value of a TextBox
that I have in a UserControl
, from the UserControl
's call in XAML. 我的最终目标是从
UserControl
在XAML中的调用中公开我在UserControl
中的TextBox
的Text
值。
<my:UserControl SetCustomText="Blah blah this is variable">
would render the UserControl
with that TextBox
's text filed in. 将使用
TextBox
的文本呈现UserControl
。
I've been working at it using various examples but I always end up with "The Property SetCustomText was not found in type UserControl" 我一直在使用各种示例工作,但我总是最终得到“在UserControl类型中找不到属性SetCustomText”
Example of how you can do this: 如何执行此操作的示例:
<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"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.