问题描述
||
我想知道是否有人可以提供帮助。我已经半天不停地解决这个问题了,我一定做错了。我有一个带有多个依赖项属性的自定义控件。
[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),typeof(InformationBubble),new PropertyMetadata(string.Empty,new PropertyChangedCallback(OnProductTitleChanged)));
我想像一下,当您使用字符串常量时,WPF会使用反射直接访问属性“公共字符串ProductTitleText \”。 DependencyProperty将被忽略,因为属性名称不匹配(\“ ProductTitle \”与\“ ProductTitleText \”)。
因此,对于标题,您有一个名为\“ ProductTitle \”的依赖项属性和一个名为\“ ProductTitleText \”的(字符串)属性。
对于ProductImage,您有一个名为\“ ProductImageSource \”的依赖项属性和一个也称为\“ ProductImageSource \”的属性(ImageSource类型)。
是否有意义?