Setting DataContext user control from code

It should be pretty simple, but it throws VS2008 for a serious cycle.

I tried WPF with MVVM and I am new to it, although I have been developing for about 15 years and have comp. Sci. power. For the current client, I have to use VB.Net.

I renamed my own variables and removed some distractions in the code below, so please forgive me if it is not 100% syntactically perfect! You probably don't need code to understand the question, but I will include it in case this helps.

I have a very simple MainView.xaml file:

<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">

<Button Name="Button1">Show Grid</Button>
<StackPanel Name="teststack" Visibility="Hidden"/>

</Window>

I also have a UserControl called DataView, which consists of a DataGrid:

<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" >
<Grid>
    <WpfToolkit:DataGrid 
        ItemsSource="{Binding Path=Entries}" SelectionMode="Extended">
    </WpfToolkit:DataGrid>
</Grid>
</UserControl>

The constructor for user control of the DataView sets the DataContext, binding it to the view model, as shown here:

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

DataView ( ViewModelBase ):

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

UserControl , XAML. , .

, , , , , XAML. StackPanel:

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

, Button1_Click, , CPU 50%.

UserControl? DataContext DataEntry. , ( , ).

Button1_Click ( DataEntry ), :

dataViewModel = New ViewModels.DataViewModel
dataViewModel.LoadDataEntries()
dataView.DataContext = dataViewModel

. - , , , , ?

.

+3
3

DataContext UserControl UserControl ( XAML, ). DataContext UserControl UserControl (IsVisibleChanged, ). UserControl XAML, "" "". Button1, UserControl Visible. , UserControl , , DataContext . , kludgey.:-( , !

+1

, , , . , , , , .

, . , ( ). , .

+1

, , :

VB.NET, #:

private void Button1_Click(Object sender, RoutedEventArgs e)
{
  Thread thr = new Thread(delegate()
  {
    DataView dataView = new DataView();

    this.Dispatcher.BeginInvoke((Action) delegate()
    {
      teststack.Children.Add(dataView);
    });
  });
  thr.Start();
}
-1
source

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


All Articles