Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用xaml构建简单对象_C#_Wpf_Xaml_Templates_Controls - Fatal编程技术网

C# 用xaml构建简单对象

C# 用xaml构建简单对象,c#,wpf,xaml,templates,controls,C#,Wpf,Xaml,Templates,Controls,我有一个简单的类,我想通过xaml简单地创建我的类的实例。但我仍然会遇到类似这样的错误:“'Test'成员无效,因为它没有限定的类型名。” UserControl1.xaml.cs: namespace WpfTestApplication1 { public class UserControl1 : UserControl { public string Test { get; set; } public UserControl1()

我有一个简单的类,我想通过xaml简单地创建我的类的实例。但我仍然会遇到类似这样的错误:“'Test'成员无效,因为它没有限定的类型名。”

UserControl1.xaml.cs:

namespace WpfTestApplication1
{
    public class UserControl1 : UserControl
    {
        public string Test { get; set; }

        public UserControl1()
        {
        }
    }
}
UserControl1.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WpfTestApplication1">
    <local:UserControl1>
        <Setter Property="Test" Value="aaaa" />
    </local:UserControl1>
</ResourceDictionary>


请提供帮助。

尝试使用而不是默认属性

正如@us3r所说的……依赖性财产是您所寻找的

你要做的是:

// Dependency Property
public static readonly DependencyProperty TestProperty = 
     DependencyProperty.Register( "Test", typeof(string),
     typeof(UserControl1 ));

// .NET Property wrapper
public string Test
{
    get { return GetValue(TestProperty ).; }
    set { SetValue(TestProperty , value); }
}
最后我发现了。 我在xaml中将控件定义为

<local:UserControl1 x:Key="xxx" Test="aaaa"/>

不使用setter和property语句,只需直接定义属性即可。
谢谢你的帮助

UserControl
上设置
属性的方式无效。通过将
Setter
放入节点内,您已经设置了
UserControl
内容

如果希望将测试定义为绑定目标,请首先将其定义为
dependencProperty
,然后直接在UserControl上将其设置为

 <local:UserControl1 Test="aaaa"/>


谢谢,但没用。我试过了,但同样的问题仍然出现。谢谢,但这没有帮助。我试过了,但同样的问题仍然出现。