none
WPF中的附加属性为什么只能是int型?别的类型如string会报错啊。 RRS feed

  • 问题

  • 代码如下,

    运行就报错,请大家看看这附加属性到底怎么回事。

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace attcchedproperties
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();

                School myschool = new School();
                Human myhuman = new Human();
                School.SetName(myhuman, "诚意");
                string myname = School.GetName(myhuman);
                MessageBox.Show(myname.ToString());
            }

            private void Button_Click(object sender, RoutedEventArgs e)
            {

            }
        }
     
        class School : DependencyObject
        {

            string name;
            int grade;

            public string Name
            {
                get
                {
                    return name;
                }

                set
                {
                    name = value;
                }
            }

            public int Grade
            {
                get
                {
                    return grade;
                }

                set
                {
                    grade = value;
                }
            }

     

            public static string GetName(DependencyObject obj)
            {
                return (string)obj.GetValue(NameProperty);
            }

            public static void SetName(DependencyObject obj, string value)
            {
                obj.SetValue(NameProperty, value);
            }

            // Using a DependencyProperty as the backing store for Name.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty NameProperty =
                DependencyProperty.RegisterAttached("Name", typeof(string), typeof(School), new PropertyMetadata(0));


        }

        class Human : DependencyObject
        {

        }

    }


    Jason

    2013年12月5日 3:00

答案

  • Hi Jason,

      欢迎来到MSDN中文论坛。

     
    DependencyProperty NameProperty = 
    
    DependencyProperty.RegisterAttached("Name", typeof(string), typeof(School), new PropertyMetadata(string.Empty));
    
    


     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2013年12月10日 4:22
    版主
  • PropertyMetadata(“0”),设置的默认值类型要与注册类型一致。
    2013年12月10日 8:29

全部回复

  • Hi Jason,

      欢迎来到MSDN中文论坛。

     
    DependencyProperty NameProperty = 
    
    DependencyProperty.RegisterAttached("Name", typeof(string), typeof(School), new PropertyMetadata(string.Empty));
    
    


     


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2013年12月10日 4:22
    版主
  • PropertyMetadata(“0”),设置的默认值类型要与注册类型一致。
    2013年12月10日 8:29