大佬教程收集整理的这篇文章主要介绍了.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
[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,所以我必须在这里遗漏一些明显的东西.
非常感谢任何帮助或见解.
短剑的一种
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类型).
是否有意义?
以上是大佬教程为你收集整理的.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型全部内容,希望文章能够帮你解决.net – ‘System.Windows.Data.Binding’类型的对象无法转换为’System.String’类型所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。