带有MVVM的两种方式的WebBrowser URL

时间:2018-08-26 14:21:51

标签: wpf mvvm webbrowser-control

我有WebBrowser Helper,它将字符串变量从ViewModel转换为WebBrowser的URL地址。

public static class WebBrowserHelper
    {
        public static readonly DependencyProperty BindableSourceProperty =
            DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserHelper), 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;
            }
        }

    }

如何获取双向更新URL?例如,如果我单击链接并将重定向到另一个页面,则ViewModel中的变量也会更改。 WebBrowser URL的字符串变量。重要的是这应该是MVVM

0 个答案:

没有答案