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,784 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
have anyone a small sample for me like this:
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
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>