Model View ViewModel in WPF using WebBrowser

I am writing an application in WPF using the MVVM template, where Im is limited to binding to exclusivley properties and commands

However, I want to use a WebBrowser control that can only accept an html string for content as an argument to mthod, not a property.

I was about to create a new control obtained from Webbrowser with the required property, but the control class is sealed. I tried to create a shell control, but I had all kinds of problems with dependency properties, which seemed to be more complex than I could worry.

Is it possible to somehow insert a parameter (string) into a method with MVVM without resorting to code in files with code (this is a big no-no).

thanks

Dean

+3
source share
3 answers

Why doesn't any code in files with a code name go ? I think this is one of the most well-known misunderstandings in the MVVM community.

MVVM is not a template for removing the code behind . This is to separate the part of the view (appearance, animation, etc.) from the logical part (workflow). Alternatively, you can unit test the logical part.

I know enough scenarios where you have to write code because data binding is not the solution to everything. Examples of applications that use the code behind and still perform MVVM separation can be found here:

WPF Application Framework (WAF)

+15
source

, ( WebBrowser, html?).

MVVM , , , .

, " + ", Click'- . , ViewModel, , Tag WebBrowser.

:

<TextBox x:Name="addressBar" /> <!-- If you use the address bar -->
<Button Content="Go" Click="NavigateButton_Click" />
<WebBrowser x:Name="browser" Tag="{Binding URL}" />  <!-- If you bind to a VM property -->

- :

void NavigateButton_Click(object sender, RoutedEventArgs e)
{
   browser.Navigate(new Uri(addressBar.Text)); // Address Bar
   browser.Navigate(new Uri(browser.Tag.ToString()); // Tag Binding
}
+5

Source: https://habr.com/ru/post/1703683/


All Articles