MicrosoftEdge / MicrosoftEdge/WebView2Feedback
Possible BUG but Please help to understand how Webview2 should work with WPF MVVM concepts.
@champnic is already working on this.
Since Apr 2, 2021.
- Dominant language
- PowerShell
- Stars
- 526
- Forks
- 67
- PR merge metrics
- No merged PRs in 30d
Description
I am working on a WPF app with dynamically added TabItems. Further I am attempting to follow MVVM concepts.
I have set up an example repo Example WPF TabControl
In the example repo the TabControl's ItemSource is set to an ObservableCollection (name: TabCollection) of a UserControl's (name: BrowserTabItemUC) view model (name: BrowserTabItemViewModel). This UserControl holds the WebView2 control.
To set up the TabItem I have a ContentTemplate as follows;
<DataTemplate x:Key="TabBrowserDataTemplate"
DataType="{x:Type local:BrowserTabItemViewModel}">
<Grid>
<local:BrowserTabItemUC />
</Grid>
</DataTemplate>
When a new tab is requested I add a new instance of the viewmodel to the TabCollection. This is where I have issues with creating the new WebView2. I am currently able to add a new Source to WebView2 from the ViewModel as Source is a Dependency property of the Webview2, however, how does one add an existing environment? This question is also my main issue with trying to implement MVVM concepts. CoreWebView2 is not a Dependency Property and thus all of these properties, methods and events are not available to a view model.
Generally I am wondering how the WebView2 should work in such a case, especially for WPF TabControl as the whole WebView2 re-initializes when manually changing tabs.
Possible bug
In my example simply close a second tab. When the collection attempts to remove the item I get an error with respect to the Source property being null. See the pic.

I cannot reproduce a second error i sometimes get. When manually changing tabs sometimes the Source become null as well and WebView2 does not like this null value as well. As i said i cannot reproduce this all the time so am assuming it is my code.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.