【问题标题】:Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'“System.Windows.Data.Binding”类型的对象无法转换为“System.String”类型
【发布时间】:2011-08-31 07:25:40
【问题描述】:

我想知道是否有人可以提供帮助。我已经在这个问题上敲了半天了,我一定是做错了什么。我有一个带有许多依赖属性的自定义控件。

    [TemplatePart(Name = InformationBubble.InformationBubbleTitlePart, Type = typeof(TextBlock))]
    [TemplatePart(Name = InformationBubble.InformationBubbleProductImagePart, Type=typeof(Image))]

    public class InformationBubble : Control 
    {
        #region Template Parts Name Constants

        /// <summary>
        /// Name constant for the Information Bubble Title Part
        /// </summary>
        public const string InformationBubbleTitlePart = "InformationBubbleTitleText";

        /// <summary>
        /// Name constant for the Information Bubble Product Image Part
        /// </summary>
        public const string InformationBubbleProductImagePart = "InformationBubbleProductImage";

        #endregion

        #region TemplateParts

        private TextBlock _Title;

        internal TextBlock Title
        {
            get { return _Title; }
            private set
            {
                _Title = value;

                if (_Title != null)
                {
                    _Title.Text = this.ProductTitleText;       
                }
            }
        }

        private Image _ProductImage;

        internal Image ProductImage
        {
            get { return _ProductImage; }
            private set
            {
                _ProductImage = value;

                if (_ProductImage != null)
                {
                    _ProductImage.Source = this.ProductImageSource;
                }
            }
        }

        #endregion

        #region Public String Product Title 

        // Dependency properties declaration
        public static readonly DependencyProperty ProductTitleProperty = DependencyProperty.Register(
            "ProductTitle",
            typeof(string),
            typeof(InformationBubble),
            new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnProductTitleChanged)));

        public static void OnProductTitleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            InformationBubble iBubble = sender as InformationBubble;

            if (iBubble.Title != null)
            {
                iBubble.Title.Text = e.NewValue as string;
            }
        }

        public string ProductTitleText
        {
            get { return GetValue(ProductTitleProperty) as string; }
            set { SetValue(ProductTitleProperty, value); }
        }

        #endregion

        #region Public Image Source Product Image

        public static readonly DependencyProperty ProductImageSourceProperty = DependencyProperty.Register(
            "ProductImageSource",
            typeof(ImageSource),
            typeof(InformationBubble),
            new PropertyMetadata(null, new PropertyChangedCallback(OnProductImageSourceChanged)));

        public static void OnProductImageSourceChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            InformationBubble iBubble = sender as InformationBubble;

            if (iBubble.ProductImage != null)
            {
                iBubble.ProductImage.Source = e.NewValue as ImageSource;
            }
        }

        public ImageSource ProductImageSource
        {
            get { return GetValue(ProductImageSourceProperty) as ImageSource; }
            set { SetValue(ProductImageSourceProperty, value); }
        }

        #endregion

        public InformationBubble()
        {
             this.DefaultStyleKey = typeof(InformationBubble);
        }

        #region Overrides

        public override void OnApplyTemplate()
        {       
            base.OnApplyTemplate();

            Title = GetTemplateChild(InformationBubble.InformationBubbleTitlePart) as TextBlock;
            ProductImage = GetTemplateChild(InformationBubble.InformationBubbleProductImagePart) as Image;
        }

        #endregion

        #region Private Methods

        private void GoToState(string stateName, bool useTransitions)
        {
            VisualStateManager.GoToState(this, stateName, useTransitions);
        }

        #endregion
    }

现在,如果我在我的 xaml 中的某处使用此控件,如果我这样做,它就会起作用:

<controls:InformationBubble 
        ProductImageSource="{Binding SelectedItem.NormalImageSource}"
        ProductTitleText="Test Title"
        "/>

但是,如果我尝试将产品标题文本数据绑定到我的 ViewModel 中 SelectedItem 对象的标题属性:

<controls:InformationBubble 
            ProductImageSource="{Binding SelectedItem.NormalImageSource}"
            ProductTitleText="{Binding SelectedItem.Title, Mode=TwoWay"
            "/>

我得到“System.Windows.Data.Binding”类型的对象无法转换为“System.String”类型。 TextBlock 的 text 属性是一个 DependencyProperty,所​​以我必须在这里遗漏一些明显的东西。

【问题讨论】:

  • 我同意 Ole 的观点,您在注册依赖属性时似乎有错字。请注意,您可以使用 sn-p 来创建依赖属性,即 propdp tab tab。将来应该会有所帮助:)

标签: .net data-binding mvvm


【解决方案1】:

会不会是物业的名字写错了。下面代码中的“ProductTitle”应该是“ProductTitleText”吗?

public static readonly DependencyProperty ProductTitleProperty = DependencyProperty.Register(
    "ProductTitle",  // "ProductTitleText" ?
    typeof(string),
    typeof(InformationBubble),
    new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnProductTitleChanged)));

我想当您使用字符串常量时,WPF 使用反射直接访问属性“公共字符串 ProductTitleText”。 DependencyProperty 被忽略,因为属性名称不匹配(“ProductTitle”与“ProductTitleText”)。

因此,对于标题,您有一个名为“ProductTitle”的依赖属性和一个名为“ProductTitleText”的(字符串)属性。 对于 ProductImage,您有一个名为“ProductImageSource”的依赖属性和一个也称为“ProductImageSource”的属性(ImageSource 类型)。

有意义吗?

【讨论】:

  • 但它使用字符串值工作,只有当我尝试将数据绑定到 ViewModel 中的对象时它才会失败。
  • 我知道这很简单!非常感谢您的帮助。我从来没有新的名字必须匹配,但显然这在数据绑定时很重要。我可以应用字符串值的事实让我震惊!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 2014-03-14
  • 2017-11-24
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多