|
|
آیا کسی می داند چگونه می توان عمل databind را برای ویژگی .Source از WebBrowser در WPF انجام داد؟(3.5SP1).من در برنامه خود یک listview دارم که میخواهم در سمت چپ آن یک WebBrowser کوچک و در سمت راست محتوا (content) را داشته باشم و می خواهم Source مربوط به هر WebBrowser را با استفاده از URL های مربوط به اعضای لیست مقدار دهی کنم.
در زیر کد مربوطه را که برای این کار استفاده کردم آوردم ، اما قسمت "<WebBrowser Source="{Binding Path=WebAddress}"" در آن کامپایل نمی شود.
<DataTemplate x:Key="dealerLocatorLayout" DataType="DealerLocatorAddress">
<StackPanel Orientation="Horizontal">
<!--Web Control Here-->
<WebBrowser Source="{Binding Path=WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200"
/>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=CompanyName}" FontWeight="Bold" Foreground="Blue" />
<TextBox Text="{Binding Path=DisplayName}" FontWeight="Bold" />
</StackPanel>
<TextBox Text="{Binding Path=Street[0]}" />
<TextBox Text="{Binding Path=Street[1]}" />
<TextBox Text="{Binding Path=PhoneNumber}"/>
<TextBox Text="{Binding Path=FaxNumber}"/>
<TextBox Text="{Binding Path=Email}"/>
<TextBox Text="{Binding Path=WebAddress}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
منبع : stackoverflow
مشکل اینجاست که ویژگی WebBrowser.Source یک ویژگی از نوع Dependency Property نیست. راه حلی که وجود دارد آن است که از یک AttachedProperty استفاده نمایید.
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string) obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
string uri = e.NewValue as string;
browser.Source = uri != null ? new Uri(uri) : null;
}
}
}
سپس در کد Xaml خود این گونه عمل کیند:
<WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
Width="300"
Height="200" />
|
|
من یک wrapper usercontrolنوشته ام که امکان استفاده از DependencyProperty ار فراهم می آورد.
XAML:
<UserControl x:Class="HtmlBox">
<WebBrowser x:Name="browser" />
</UserControl>
C#:
public static readonly DependencyProperty HtmlTextProperty = DependencyProperty.Register("HtmlText", typeof(string), typeof(HtmlBox));
public string HtmlText {
get { return (string)GetValue(HtmlTextProperty); }
set { SetValue(HtmlTextProperty, value); }
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
base.OnPropertyChanged(e);
if (e.Property == HtmlTextProperty) {
DoBrowse();
}
}
private void DoBrowse() {
if (!string.IsNullOrEmpty(HtmlText)) {
browser.NavigateToString(HtmlText);
}
}
این هم نحوهی استفادهاش:
<controls:htmlbox htmltext="{Binding MyHtml}"/>
تنها مشکلی که اینجا وجود دارد اینست که کنترل WebBrowser ما کاملا WPF نخواهد بود... تنها یک wrapper برای یک کامپوننت win32 می باشد. این مساله باعث می شود که خاصیت z-index برای آن عمل نکرده و در نتیجه همواره در روی تمام المان ها قرار می گیرد( بعنوان مثال : در یک scrollviewer این مساله می تواند مشکل ساز باشد). مطالب بیشتر در زمینه مطالب مرتبط با Win32-WPF را می توان در MSDN مشاهده کرد:
http://msdn.microsoft.com/en-us/library/ms742522.aspx
این همان چیزی است که من برای نمایش HTML خود به آن نیاز داشتم.... آسان، تمیز و تقریبا عملکرد آن را نیز فهمیدم.