i'm new to WPF and the MVVM pattern, i'm trying to make an app that uses several controls so i create each control separately and i am facing some difficulty for how to share data between controls
lets say i have a control that has a label and another control that contains a textbox, in the main window i want when i add the two custom controls i need the label control to show what i'm typing in the text box, i know how to implement that if i use the label and textbox directly in my window but i need that to solve similar issue, here is the Label Control
<UserControl x:Class="TestWPF2.Views.LabelControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Label ></Label> </Grid> </UserControl> TextBox Custom control
<UserControl x:Class="TestWPF2.Views.TextBoxControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <TextBox ></TextBox> </Grid> </UserControl> and this is the window code
<Window x:Class="TestWPF2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:V="clr-namespace:TestWPF2.Views" xmlns:Controls="clr-namespace:TestWPF2.Views" Title="MainWindow" Height="350" Width="525"> <DockPanel LastChildFill="True"> <Controls:TextBoxControl ></Controls:TextBoxControl> <Controls:LabelControl ></Controls:LabelControl> </DockPanel> </Window>
UserControlsimply to wrap a single control like aLabelorTextBox; just instantiate theLabelorTextBoxdirectly. I don't know if your actual code does this, or if this was just a simplified example.