C# 用xaml构建简单对象
我有一个简单的类,我想通过xaml简单地创建我的类的实例。但我仍然会遇到类似这样的错误:“'Test'成员无效,因为它没有限定的类型名。” UserControl1.xaml.cs: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()
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"/>
谢谢,但没用。我试过了,但同样的问题仍然出现。谢谢,但这没有帮助。我试过了,但同样的问题仍然出现。