WPF ViewModel DataContext between UserControl Windows

Bernd Riemke 41 Reputation points
2020-06-15T09:26:46.217+00:00

Hi,
have anyone a small sample for me like this:

  1. Master Window
  2. Sub Window
  3. In the Sub Window is a UserControl Window

How can i send data via datacontext from the Master Window to the UserControl Window?
I can set the first data easy from the Master Window to the Sub Window
But from the Sub Window i can not set the datacontext with my data from the Sub Window

Have anyone a small sample how i can send an get data from the UserControl Window?

Best regards

Bernd

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,685 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-06-15T11:52:55.69+00:00

    Hi, if you use the same instance of ViewModel for Master and Child Window you can bind Controls to the same property in ViewModel (instance).

    Try following demo:

    <Window x:Class="Window030"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Master Window" Height="450" Width="800">
      <StackPanel>
        <TextBox Text="{Binding Info, UpdateSourceTrigger=PropertyChanged}"/>
      </StackPanel>
    </Window>
    

    Imports System.ComponentModel
    Imports System.Runtime.CompilerServices
    
    Public Class Window030
    
      Public Sub New()
    
        ' This call is required by the designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        Dim vm As New WpfApp030.ViewModel
        Me.DataContext = vm
        Call (New Window030Child With {.DataContext = vm}).Show()
      End Sub
    
    End Class
    
    Namespace WpfApp030
    
      Public Class ViewModel
        Implements INotifyPropertyChanged
    
        Private _info As String
        Public Property Info As String
          Get
            Return Me._info
          End Get
          Set(value As String)
            Me._info = value
            OnPropertyChanged()
          End Set
        End Property
    
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
        Friend Sub OnPropertyChanged(<CallerMemberName> Optional propName As String = "")
          RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
        End Sub
    
      End Class
    

    <Window x:Class="Window030Child"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="Child Window" Height="450" Width="800">
      <StackPanel>
        <TextBox Text="{Binding Info, UpdateSourceTrigger=PropertyChanged}"/>
      </StackPanel>
    </Window>
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful