Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Currently, I have a working C# WPF application consisting of several User Controls. One of the User Controls needs data from this MainWindow to properly display additional information and process itself. (For now, it would be satisfactory just to pass a string between the MainWindow and the User Control.) I already am able via a Dependency Property (in the UC code behind) to bind the data to the User Control XAML, and it displays fine. However, I am unable to access that same data (that is being displayed in the view model that is tied to the UC.

Here are related code snippets:

MainWindow XAML:

XML
<UCV:SetupUC UC1="{Binding DataContext.ContainerData, ElementName=winCon, Mode=TwoWay}">
</UCV:SetupUC>


SetupUC XAML (this is the User Control):

<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding UC1, ElementName=root}" VerticalAlignment="Top" Width="120"/>

Note, this is displaying fine, including changing as the value changes in the MainWindow.

SetupUC Code behind:

C#
public partial class SetupUC : UserControl, INotifyPropertyChanged
    {
public static readonly DependencyProperty UC1Property = DependencyProperty.Register("UC1", typeof(string),
            typeof(SetupUC), new PropertyMetadata(OnUC1Changed));
        public string UC1
        {
            get { return (string)GetValue(UC1Property); }
            set
            {
                SetValue(UC1Property, value);
            }
        }

public SetupUC()
        {
            InitializeComponent();
            SetupViewModel svm = new SetupViewModel(UC1);
            this.DataContext = svm;
        }
}



Finally, SetupViewModel:

C#
public class SetupViewModel : INotifyPropertyChanged
   {
       public SetupViewModel(string UC2)
       {
           ContainerData = UC2;
           _UserProfile = new UserProfile();
           UserProfile.Pwd = ContainerData;
       }
public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private string _containerData;
        public string ContainerData
        {
            get { return _containerData; }
            set
            {
                _containerData = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("ContainerData"));
                }
            }
        }
    }


UC1 in the code behind which does display in the SetupUC view, never seems to pass to the view model, UC2. Interestingly enough, if I replace the UC1 in the code behind with a string like "123" it will get passed to the VM in UC2.

I am pretty new to C# and MVVM, and I have been googling this for days. Found a lot of posts and have trid different recs. from those posts, but still no luck...

Help, please?!?
Posted

1 solution

XML
Should the binding in the user control for the text box be ContainerData instead of UC1?

<TextBox x:Name="tbx1" HorizontalAlignment="Left" Height="23" Margin="159,22,0,0" TextWrapping="Wrap" Text="{Binding ContainerData, Mode=TwoWay}" VerticalAlignment="Top" Width="120"/>
 
Share this answer
 
Comments
Member 10558493 21-Jul-14 15:34pm    
Pretty sure not - in any event the data from the main window is displaying in the User Control textbox (tbx1). What I cannot seem to do is access the text in that textbox from the View Model, SetupViewModel.cs. Note, the UC1 dependency property is in the user control code behind, SetupUC.cs. (All posts I have found indicate that DP's should be defined in the code behind and not the VM).
Member 10558493 23-Jul-14 19:31pm    
anyone?

help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900