WPF - Cannot convert '<null>' from type '<null>' to type 'System.Uri'
I have a simple custom control to display a hyperlink in a text block:
LinkTextBlock.xaml:
<TextBlock >
<Hyperlink NavigateUri="{Binding Url, ElementName=root}" >
<TextBlock Text="{Binding Text, ElementName=root}" />
</Hyperlink>
</TextBlock>
LinkTextBlock.xaml.cs:
public static readonly DependencyProperty UrlProperty = DependencyProperty.Register("Url", typeof (string), typeof (LinkTextBlock));
public string Url
{
get { return (string) GetValue(UrlProperty); }
set { SetValue(UrlProperty, value); }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof (string), typeof (LinkTextBlock));
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
Then in the DataTemplate for the ListBox, I have:
<Controls:LinkTextBlock Text="{Binding Email}" Url="{Binding Email}" />
When I run the application, it works fine. The control displays the hyperlinks correctly and there are no obvious problems. However, when I look at the Exit window, I get exceptions, one for each ListBox:
System.Windows.Data error: 22: Unable to convert 'from type' to type 'System.Uri' for culture 'en-US' with default conversions; viewed using the Converter Binding property. NotSupportedException: 'System.NotSupportedException: UriTypeConverter cannot convert from (zero). at System.ComponentModel.TypeConverter.GetConvertFromException (Object Value) when System.ComponentModel.TypeConverter.ConvertFrom (ITypeDescriptorContext Context, CultureInfo Object, Object Value) when System.UriTypeConverter.ConvertFrom (Object.UriTypeConverter.ConvertFrom (ITypeDescriptor, CultureContext) when System.UriTypeConverter. .Data.DefaultValueConverter.ConvertHelper (Object o, Type destinationType, DependencyObject targetElement, Culture CultureInfo, Boolean isForward)
Why is this happening? I know the binding error is the result of binding to NavigateURI. Do you have any suggestions for me? What can I do about it? I really appreciate your inputs.
thanks
source to share
Shouldn't it
<TextBlock >
<Hyperlink NavigateUri="{Binding Url, ElementName=root}" >
<TextBlock Text="{Binding Text, ElementName=root}" />
</Hyperlink>
</TextBlock>
this is?
<TextBlock Text="{Binding Text, ElementName=root}">
<Hyperlink NavigateUri="{Binding Url, ElementName=root}" />
</TextBlock>
source to share