dotnet / dotnet/wpf

DataContext is "lost" for content of ScrollViewer when doing async stuff

Open
#3,504 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: SDK 3.1.402 (3.1.8)
* Windows version: Windows 10 Version 1809 (17763.107)
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio: No

**Problem description:**
We are using a simple WPF UserControl with a single ScrollViewer and TextBlock inside. We are then showing this UserControl within a Window using ContentPresenter. When doing some async stuff in-between the data binding "breaks". Detailed steps below:

1. Creating the UserControl
2. Creating the view model
3. await Task.Delay(100)
4. Setting the view model as DataContext of the UserControl
5. Setting the UserControl as Content of the ContentPresenter

**Actual behavior:**
The data binding does not work. Observing the problem with Snoop we can see that that DataContext is not set on the TextBlock, which is the reason why the text is not shown.

**Expected behavior:**
The data binding should work (DataContext should be available for the inner TextBlock

**Remarks**

* If the Task.Delay is removed, the binding works.
* If the ScrollViewer is removed the binding works.

* If "vm.IsEnabled = true" is called before the await and "vm.isEnabled = false" is called after "view.DataContext = vm" the binding works.

**Minimal repro:**
[Binding_Issue_Demo.zip](https://github.com/dotnet/wpf/files/5216572/Binding_Issue_Demo.zip)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.