类型'System.Windows.Data.Binding“的对象无法转换为类型”System.String'的类型、转换为、对象、System

2023-09-05 01:38:50 作者:楼空,封存浮生。

我不知道,如果有人可以提供帮助。我一直在敲我的头靠在这个问题了半天,现在,我必须做一些错误的。我有一些依赖属性的自定义控件。

I wonder if anyone can help. I have been banging my head against this problem for half a day now, I must be doing something wrong. I have a custom control with a number of dependency properties.

[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中的某处使用此控制它的工作原理,如果我这样做:

Now if I use this control somewhere in my xaml it works if I do this:

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

但是,如果我尝试和数据绑定产品标题文本到的SelectedItem对象在我的视图模型的称号属性:

But if I try and data bind the product title text to the title property of the SelectedItem object in my ViewModel:

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

我收到的类型System.Windows.Data.Binding的对象无法转换为类型System.String'的。 TextBlock的Text属性是一个DependencyProperty的,所以我必须缺少明显的东西在这里。

I get Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'. The text property of a TextBlock is a DependencyProperty so I must be missing something obvious here.

任何帮助或洞察力是非常AP preciated。

Any help or insight is much appreciated.

克里斯

推荐答案

难道说该属性的名称是错误的。应在code中的ProductTitle的也许是ProductTitleText?

Could it be that the name of the property is wrong. Should the "ProductTitle" in the code below perhaps be "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)。

I imagine that when you use a string constant, then WPF uses reflection to access the property "public string ProductTitleText" directly. The DependencyProperty is ignored, as the name of the property do not match ("ProductTitle" vs. "ProductTitleText").

因此​​,对于标题,你有一个名为ProductTitle依赖属性,并称为ProductTitleText一个(String)属性。 对于ProductImage你有一个依赖proeprty称为ProductImageSource和属性(类型的ImageSource的),也被称为ProductImageSource。

So for the title you have a dependency property called "ProductTitle" and a (string) property called "ProductTitleText". For the ProductImage you have a dependency proeprty called "ProductImageSource" and a property (of type ImageSource) also called "ProductImageSource".

是否有意义?