XAML属性值设置为用户控件 [英] Setting XAML property value to user control

查看:173
本文介绍了XAML属性值设置为用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF用户控件,我想要一个文本的它是从它使用XAML读取标签。因此..

I have a user control in WPF which i want the text of one of it's labels to be read from the XAML where it is used. Hence..

我的用户控制:

 <UserControl x:Class="muc">
        <Label Foreground="#FF7800" FontSize="20" FontWeight="Bold">          
             <Label.Content>
                <Binding ElementName="TestName" Path="." />
             </Label.Content>
        </Label>
 </UserControl>

然后使用它:

 <mycontorls:muc TestName="This is a test" />

不过,这并不工程...
我怎样才能读取性能?

But it doesn't works ... How can i read the properties ?

推荐答案

我只使用Silverlight做到了这一点,但如果它工作在相同的方式,我不会是惊讶!

I've only done this with Silverlight, but i wouldnt be surprised if it works in the exact same way!

// <summary>
// Xaml exposed TextExposedInXaml property.
// </summary>
public static readonly DependencyProperty TestNameProperty = DependencyProperty.Register("TestName", typeof(string), typeof(NameOfMyUserControl), new PropertyMetadata(string.empty));

// <summary>
 // Gets or sets the control's text
// </summary>
public string TextExposedInXaml
{
            get
            {
                return (string)GetValue(TestNameProperty );
            }

            set
            {
                SetValue(TestNameProperty , value);

                // set the value of the control's text here...!
            }
}

这篇关于XAML属性值设置为用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆