I am coding in C#, using the MVVM pattern. I have a Main Window that uses several User Controls. I am trying to pass string data fro the Main Window to the View Model of the User Control. So far I have been able to pass the Main Window data to the User Control View and View code behind. However, I can't seem to expose that data to the User Control View Model. Here is what does work:
Main Window XAML,
UCV:SetupUC UC1="{Binding DataContext.ContainerData, ElementName=winCon, Mode=TwoWay}">
</UCV:SetupUC>
User Control XAML,
<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.017,0.304"/>
User Control XAML code behind,
public partial class SetupUC : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty UC1Property =
DependencyProperty.Register(
"UC1", typeof(string), typeof(SetupUC),
new FrameworkPropertyMetadata()
{
PropertyChangedCallback = OnUC1Changed,
BindsTwoWayByDefault = true
});
public string UC1
{
get { return (string)GetValue(UC1Property); }
set
{
SetValue(UC1Property, value);
}
}
private static void OnUC1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
// code to be executed on value update
}
}
public SetupUC()
{
InitializeComponent();
SetupViewModel svm = new SetupViewModel();
this.DataContext = svm;
Binding binding = new Binding("ViewModelStringProperty") { Source = svm, Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(this, SetupUC.UC1Property, binding);
}
The last 2 lines above are an attempt to bind the DP to a property in the User Control View Model - which other posts have suggested as a possible solution. I have not been able to get the binding to work. Again, the TextBox x:Name="tbx1" is displaying the DP, UC1, just fine; it changes "realtime" when the bound data in the Main Window changes.
Main Window XAML,
UCV:SetupUC UC1="{Binding DataContext.ContainerData, ElementName=winCon, Mode=TwoWay}">
</UCV:SetupUC>
User Control XAML,
<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.017,0.304"/>
User Control XAML code behind,
public partial class SetupUC : UserControl, INotifyPropertyChanged
{
public static readonly DependencyProperty UC1Property =
DependencyProperty.Register(
"UC1", typeof(string), typeof(SetupUC),
new FrameworkPropertyMetadata()
{
PropertyChangedCallback = OnUC1Changed,
BindsTwoWayByDefault = true
});
public string UC1
{
get { return (string)GetValue(UC1Property); }
set
{
SetValue(UC1Property, value);
}
}
private static void OnUC1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue)
{
// code to be executed on value update
}
}
public SetupUC()
{
InitializeComponent();
SetupViewModel svm = new SetupViewModel();
this.DataContext = svm;
Binding binding = new Binding("ViewModelStringProperty") { Source = svm, Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(this, SetupUC.UC1Property, binding);
}
The last 2 lines above are an attempt to bind the DP to a property in the User Control View Model - which other posts have suggested as a possible solution. I have not been able to get the binding to work. Again, the TextBox x:Name="tbx1" is displaying the DP, UC1, just fine; it changes "realtime" when the bound data in the Main Window changes.