Rus*_*uss 85 c# browser data-binding wpf xaml
有谁知道如何在WPF(3.5SP1)中数据绑定WebBrowser的.Source属性?我有一个listview,我希望在左边有一个小的WebBrowser,在右边有内容,并在每个WebBrowser的源中为每个绑定到列表项的对象中的URI进行数据绑定.
这是我迄今为止的概念证明,但" <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>
Run Code Online (Sandbox Code Playgroud)
Tod*_*ite 154
问题是WebBrowser.Source不是DependencyProperty.一种解决方法是使用一些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 = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的xaml做:
<WebBrowser ns:WebBrowserUtility.BindableSource="{Binding WebAddress}"/>
Run Code Online (Sandbox Code Playgroud)
Sam*_*ack 33
我已经修改了Todd的优秀答案,以生成一个版本来处理绑定源中的字符串或Uris:
public static class WebBrowserBehaviors
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(object), typeof(WebBrowserBehaviors), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static object GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, object value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser == null) return;
Uri uri = null;
if (e.NewValue is string )
{
var uriString = e.NewValue as string;
uri = string.IsNullOrWhiteSpace(uriString) ? null : new Uri(uriString);
}
else if (e.NewValue is Uri)
{
uri = e.NewValue as Uri;
}
browser.Source = uri;
}
Run Code Online (Sandbox Code Playgroud)
Roe*_*elF 30
我写了一个包装器usercontrol,它使用了DependencyProperties:
XAML:
<UserControl x:Class="HtmlBox">
<WebBrowser x:Name="browser" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
<Controls:HtmlBox HtmlText="{Binding MyHtml}" />
Run Code Online (Sandbox Code Playgroud)
这个问题的唯一问题是WebBrowser控件不是"纯粹的"wpf ......它实际上只是win32组件的包装器.这意味着控件将不会尊重z-index,并且将始终覆盖其他元素(例如:在滚动查看器中这可能会导致一些问题)有关MSDN上这些win32-wpf问题的更多信息