.net 类型为'的对象;System.Windows.Data.Binding';无法转换为类型';System.String';

.net 类型为'的对象;System.Windows.Data.Binding';无法转换为类型';System.String';,.net,data-binding,mvvm,.net,Data Binding,Mvvm,我想知道是否有人能帮忙。我已经为这个问题绞尽脑汁半天了,我一定是做错了什么。我有一个带有许多依赖属性的自定义控件 [TemplatePart(Name=InformationBubble.InformationBubbleTitlePart,Type=typeof(TextBlock))] [TemplatePart(名称=InformationBubble.InformationBubbleProductImagePart,类型=类型(图像))] 公共类信息泡沫:控制 { #区域模板零件名称常

我想知道是否有人能帮忙。我已经为这个问题绞尽脑汁半天了,我一定是做错了什么。我有一个带有许多依赖属性的自定义控件

[TemplatePart(Name=InformationBubble.InformationBubbleTitlePart,Type=typeof(TextBlock))]
[TemplatePart(名称=InformationBubble.InformationBubbleProductImagePart,类型=类型(图像))]
公共类信息泡沫:控制
{
#区域模板零件名称常量
/// 
///信息气泡标题部分的名称常量
/// 
public const字符串InformationBubbleTitlePart=“InformationBubbleTitleText”;
/// 
///信息气泡产品图像部件的名称常量
/// 
public const string InformationBubbleProductImagePart=“InformationBubbleProductImage”;
#端区
#区域模板零件
私有文本块标题;
内部文本块标题
{
获取{return\u Title;}
专用设备
{
_标题=价值;
如果(_Title!=null)
{
_Title.Text=this.ProductTitleText;
}
}
}
私人形象(ProductImage);;
内部图像产品图像
{
获取{return\u ProductImage;}
专用设备
{
_产品形象=价值;
如果(_ProductImage!=null)
{
_ProductImage.Source=this.ProductImageSource;
}
}
}
#端区
#地区公共字符串产品名称
//依赖属性声明
公共静态只读DependencyProperty ProductTitleProperty=DependencyProperty.Register(
“产品名称”,
类型(字符串),
类型(信息气泡),
新的PropertyMetadata(string.Empty,新的PropertyChangedCallback(OnProductTitleChanged));
公共静态无效OnProductTitleChanged(DependencyObject发送者,DependencyPropertyChangedEventArgs e)
{
InformationBubble IBUBLE=发送方为InformationBubble;
if(iBubble.Title!=null)
{
iBubble.Title.Text=e.NewValue作为字符串;
}
}
公共字符串ProductTitleText
{
获取{返回GetValue(ProductTitleProperty)作为字符串;}
set{SetValue(ProductTitleProperty,value);}
}
#端区
#区域公共形象源产品形象
公共静态只读DependencyProperty ProductImageSourceProperty=DependencyProperty.Register(
“ProductImageSource”,
类型(图像源),
类型(信息气泡),
新PropertyMetadata(null,新PropertyChangedCallback(OnProductImageSourceChanged));
公共静态无效OnProductImageSourceChanged(DependencyObject发送者,DependencyPropertyChangedEventArgs e)
{
InformationBubble IBUBLE=发送方为InformationBubble;
if(iBubble.ProductImage!=null)
{
IBUBLE.ProductImage.Source=e.NewValue作为ImageSource;
}
}
公共ImageSource产品ImageSource
{
获取{返回GetValue(ProductImageSourceProperty)作为ImageSource;}
set{SetValue(ProductImageSourceProperty,value);}
}
#端区
公共信息泡沫()
{
this.DefaultStyleKey=typeof(InformationBubble);
}
#区域覆盖
应用程序模板()上的公共重写无效
{       
base.OnApplyTemplate();
Title=GetTemplateChild(InformationBubble.InformationBubbleTitlePart)作为文本块;
ProductImage=GetTemplateChild(InformationBubble.InformationBubbleProductImagePart)作为图像;
}
#端区
#区域私有方法
私有void GoToState(字符串stateName,bool-usetransforms)
{
VisualStateManager.GoToState(this、stateName、useTransforts);
}
#端区
}
现在,如果我在xaml中的某个地方使用此控件,如果我这样做,它就会工作:

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


无法将“System.Windows.Data.Binding”类型的对象转换为“System.String”类型。TextBlock的text属性是DependencyProperty,因此我必须在这里遗漏一些明显的内容。

可能是属性名称错误。下面代码中的“ProductTitle”是否应该是“ProductTitleText”

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

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


这有意义吗?

但它使用字符串值工作,只有当我尝试将数据绑定到ViewModel中的对象时,它才会失败。我知道这很简单!非常感谢你的帮助。我从不认为新名称必须匹配,但显然这在数据绑定时很重要。我可以应用一个字符串值,这让我很吃惊!我同意Ole,当您注册依赖属性时,您似乎有一个输入错误。请注意,您可以使用片段创建依赖项属性,即
propdp选项卡
。这将在未来有所帮助:)
<controls:InformationBubble 
            ProductImageSource="{Binding SelectedItem.NormalImageSource}"
            ProductTitleText="{Binding SelectedItem.Title, Mode=TwoWay"
            "/>
public static readonly DependencyProperty ProductTitleProperty = DependencyProperty.Register(
    "ProductTitle",  // "ProductTitleText" ?
    typeof(string),
    typeof(InformationBubble),
    new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnProductTitleChanged)));