【问题标题】:Simple object building by xamlxaml 的简单对象构建
【发布时间】:2013-10-27 01:06:54
【问题描述】:

我有一个简单的类,我想通过 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>

请帮忙。

【问题讨论】:

    标签: c# wpf xaml templates controls


    【解决方案1】:

    您在UserControl 上设置Property 的方式无效。您已通过将Setter 放入节点中来设置UserControlContent

    如果您希望它成为绑定目标,首先将Test定义为DependencyProperty,然后直接在UserControl上将其设置为

     <local:UserControl1 Test="aaaa"/>
    

    【讨论】:

      【解决方案2】:

      尝试使用DependencyProperty 代替默认属性。

      【讨论】:

      • 谢谢,但没有帮助。我试过了,还是一样的问题。
      【解决方案3】:

      正如@us3r 所说...DependencyProperty 就是您要寻找的。​​p>

      你要做的是:

      // 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); }
      }
      

      【讨论】:

      • 谢谢,但这没有帮助。我试过了,还是一样的问题。
      【解决方案4】:

      我终于找到了。 我将 xaml 中的控件定义为

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

      不使用setter和property语句,直接定义属性即可。 感谢您的帮助!

      【讨论】:

        猜你喜欢
        • 2015-05-13
        • 2017-04-13
        • 1970-01-01
        • 2015-03-22
        • 1970-01-01
        • 1970-01-01
        • 2013-12-20
        • 1970-01-01
        • 2023-04-05
        相关资源
        最近更新 更多