加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net – ‘System.Windows.Data.Binding’类型的对象无法转换为

发布时间:2020-12-14 03:57:37 所属栏目:Windows 来源:网络整理
导读:我想知道是否有人可以帮忙.我现在已经半天不停地反对这个问题了,我一定是做错了.我有一个具有许多依赖项属性的自定义控件. [TemplatePart(Name = InformationBubble.InformationBubbleTitlePart,Type = typeof(TextBlock))][TemplatePart(Name = Information
我想知道是否有人可以帮忙.我现在已经半天不停地反对这个问题了,我一定是做错了.我有一个具有许多依赖项属性的自定义控件.

[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),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对象的title属性的数据:

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

我得到’System.Windows.Data.Binding’类型的对象不能转换为’System.String’类型. TextBlock的text属性是DependencyProperty,所??以我必须在这里遗漏一些明显的东西.

非常感谢任何帮助或见解.

短剑的一种

解决方法

可能是该物业的名称是错误的.下面代码中的“ProductTitle”应该是“ProductTitleText”吗?

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

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

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

是否有意义?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读