在C#中简单的依赖属性和用户控件问题 [英] Simple Dependency Property and UserControl issues in C#
本文介绍了在C#中简单的依赖属性和用户控件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的最终目标是揭露一个文本框
,我有一个<$ C $的文本
价值C>用户控件,从用户控件
的XAML中的电话。
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.
<my:UserControl SetCustomText="Blah blah this is variable">
将呈现用户控件
与文本框
的文本备案。
我已经在它的工作使用不同的例子,但我总是落得
I've been working at it using various examples but I always end up with "The Property SetCustomText was not found in type UserControl"
推荐答案
你如何能做到这一点例发现b
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"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<!-- Text is being bound to outward representative property;
Note the DataContext of the UserControl -->
<TextBox Text="{Binding MyTextProperty}"/>
</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"/>
这篇关于在C#中简单的依赖属性和用户控件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文