bovender framework
C# framework that implements MVVM and more
BindingWebBrowser.cs
1 /* BindingWebBrowser.cs
2  * part of Daniel's XL Toolbox NG
3  *
4  * Copyright 2014-2018 Daniel Kraus
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 using System.Windows;
23 using System.Windows.Controls;
24 using System.Windows.Resources;
25 using System.IO;
26 
27 namespace Bovender.Mvvm
28 {
40  public static class BindingWebBrowser
41  {
42  #region Attached property 'Html'
43 
44  public static readonly DependencyProperty HtmlProperty =
45  DependencyProperty.RegisterAttached(
46  "Html",
47  typeof(string),
48  typeof(BindingWebBrowser),
49  new UIPropertyMetadata(null, HtmlPropertyChanged));
50 
51  public static string GetHtml(DependencyObject obj)
52  {
53  return (string)obj.GetValue(HtmlProperty);
54  }
55 
56  public static void SetHtml(DependencyObject obj, string value)
57  {
58  obj.SetValue(HtmlProperty, value);
59  }
60 
61  public static void HtmlPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
62  {
63  WebBrowser browser = o as WebBrowser;
64  if (browser != null)
65  {
66  browser.NavigateToString(e.NewValue as string);
67  }
68  }
69 
70  #endregion
71 
72  #region Attached property 'Stream'
73 
74  public static readonly DependencyProperty StreamProperty =
75  DependencyProperty.RegisterAttached(
76  "Stream",
77  typeof(Stream),
78  typeof(BindingWebBrowser),
79  new UIPropertyMetadata(null, StreamPropertyChanged));
80 
81  public static string GetStream(DependencyObject obj)
82  {
83  return (string)obj.GetValue(StreamProperty);
84  }
85 
86  public static void SetStream(DependencyObject obj, string value)
87  {
88  obj.SetValue(StreamProperty, value);
89  }
90 
91  public static void StreamPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
92  {
93  WebBrowser browser = o as WebBrowser;
94  if (browser != null)
95  {
96  Stream s = e.NewValue as Stream;
97  if (s != null)
98  {
99  s.Seek(0, SeekOrigin.Begin);
100  browser.NavigateToStream(s);
101  }
102  }
103  }
104 
105  #endregion
106  }
107 }
Provides attached properties to facilitate data binding of a WebBrowser control.