Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting User Control's DataContext from Code-Behind
    primarykey
    data
    text
    <p>This should be pretty easy, but it throws VS2008 for a serious loop.</p> <p>I'm trying out WPF with MVVM, and am a total newbie at it although I've been developing for about 15 years, and have a comp. sci. degree. At the current client, I am required to use VB.Net. </p> <p>I have renamed my own variables and removed some distractions in the code below, so please forgive me if it's not 100% syntactically perfect! You probably don't really need the code to understand the question, but I'm including it in case it helps.</p> <p>I have a very simple MainView.xaml file:</p> <pre><code>&lt;Window x:Class="MyApp.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Main Window" Height="400" Width="800" Name="MainWindow"&gt; &lt;Button Name="Button1"&gt;Show Grid&lt;/Button&gt; &lt;StackPanel Name="teststack" Visibility="Hidden"/&gt; &lt;/Window&gt; </code></pre> <p>I also have a UserControl called DataView that consists of a DataGrid:</p> <pre><code>&lt;UserControl x:Class="MyApp.Views.DataView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfToolkit="http://schemas.microsoft.com/wpf/2008/toolkit" &gt; &lt;Grid&gt; &lt;WpfToolkit:DataGrid ItemsSource="{Binding Path=Entries}" SelectionMode="Extended"&gt; &lt;/WpfToolkit:DataGrid&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>The constructor for the DataView usercontrol sets up the DataContext by binding it to a view model, as shown here:</p> <pre><code>Partial Public Class DataView Dim dataViewModel As ViewModels.DataViewModel Public Sub New() InitializeComponent() dataViewModel = New ViewModels.DataViewModel dataViewModel.LoadDataEntries() DataContext = dataViewModel End Sub End Class </code></pre> <p>The view model for DataView looks like this (there isn't much in ViewModelBase):</p> <pre><code>Public Class DataViewModel Inherits ViewModelBase Public Sub New() End Sub Private _entries As ObservableCollection(Of DataEntryViewModel) = New ObservableCollection(Of DataEntryViewModel) Public ReadOnly Property Entries() As ObservableCollection(Of DataEntryViewModel) Get Return _entries End Get End Property Public Sub LoadDataEntries() Dim dataEntryList As List(Of DataEntry) = DataEntry.LoadDataEntries() For Each dataentry As Models.DataEntry In dataEntryList _entries.Add(New DataEntryViewModel(dataentry)) Next End Sub End Class </code></pre> <p>Now, this UserControl works just fine if I instantiate it in XAML. When I run the code, the grid shows up and populates it just fine.</p> <p>However, the grid takes a long time to load its data, and I want to create this user control programmatically after the button click rather than declaratively instantiating the grid in XAML. I want to instantiate the user control, and insert it as a child of the StackPanel control:</p> <pre><code>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click Dim dataView As New DataView teststack.Children.Add(dataView) End Sub </code></pre> <p>When I do this, as soon as the Button1_Click finishes, my application locks up, starts eating RAM, and hits the CPU about 50%.</p> <p>Am I not instantiating my UserControl properly? It all seems to come down to the DataContext assignment in DataEntry's constructor. If I comment that out, the app works as expected (without anything in the grid, of course).</p> <p>If I move this code block into Button1_Click (basically moving DataEntry's constructor code up a level), the app still fails:</p> <pre><code>dataViewModel = New ViewModels.DataViewModel dataViewModel.LoadDataEntries() dataView.DataContext = dataViewModel </code></pre> <p>I'm stumped. Can anybody give me some tips on what I could be doing wrong, or even how to debug what infinite loop my app is getting itself into?</p> <p>Many thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload