Reputation: 1546
I have a html in my property Description. When I bind this property with a textblock (ex texthide), it shows the html in textblock. But I can't bind the WebBrowser with this property. How can I bind html string to WebBrowser?
<ScrollViewer
HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto"
Margin="25, 0, 0, 0"
Grid.Row="0">
<StackPanel Orientation="Vertical">
<TextBlock
x:Name="TextHide"
Text="{Binding Path=Descrption}"
Style="{StaticResource servicesText}"
TextWrapping="Wrap" />
<phone:WebBrowser
Source="{Binding Descrption}"
x:Name="webBrowserHTML"
Foreground="Black"
Loaded="webBrowserHTML_Loaded" />
<!--<Image Source="../Images/cont_banner.png" Width="270" Grid.Row="1"/>-->
<Button Grid.Row="1">
<Button.Background>
<ImageBrush ImageSource="../Images/cont_banner.png" />
</Button.Background>
<Button.Content>
<HyperlinkButton Content="" NavigateUri="callto:3950" />
</Button.Content>
</Button>
</StackPanel>
</ScrollViewer>
Any ideas please? Best regards
Upvotes: 1
Views: 1335
Reputation: 15268
In order to be able to bind HTML directly to the WebBrowser
control, you have to create an attached property:
namespace YourAppNamespace
{
public static class WebBrowserHelper
{
public static readonly DependencyProperty HtmlProperty = DependencyProperty.RegisterAttached(
"Html", typeof(string), typeof(WebBrowserHelper), new PropertyMetadata(OnHtmlChanged));
public static string GetHtml(DependencyObject dependencyObject)
{
return (string)dependencyObject.GetValue(HtmlProperty);
}
public static void SetHtml(DependencyObject dependencyObject, string value)
{
dependencyObject.SetValue(HtmlProperty, value);
}
private static void OnHtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var browser = d as WebBrowser;
if (browser == null)
return;
var html = e.NewValue.ToString();
browser.NavigateToString(html);
}
}
}
Add the required XAML Namespace declaration:
xmlns:cxi="clr-namespace:YourAppNamespace"
And use it like this:
<phone:WebBrowser cxi:WebBrowserHelper.Html="{Binding Question.Body}" />
Upvotes: 5