Data into textbox

Jul 10, 2014 at 9:46 PM
I have a XAML window that I am trying to place information into. Right now I want the name of the patient to go into the textbox. I'm using the binding function in the UI "{Binding FullName}". The UI window pops up fine when I run it in Eclipse but the data doesn't get displayed. Below is the class I'm using which is being called with "var vm = new Class2(context.Patient);" on the Execute code in the VMS.TPS namespace. Below is Class 2:

namespace ClassLibrary1
{
   public class Class2
   {
         private VMS.TPS.Common.Model.API.Patient _patient;

         public Class2(VMS.TPS.Common.Model.API.Patient patient)
         {
                     this. _patient = patient;
                     FullName = _patient.Name;
           }

           public string FullName {get; set}

         }
}

Any thoughts would be helpful!
Jul 11, 2014 at 2:19 PM
You have to make sure your XAML window "knows about" the object you want to bind to.

Suppose your XAML class is defined like this:
    public partial class MyUI : UserControl
    {
        public MyUI()
        {
            InitializeComponent();
        }
    }
In your main script, you can instantiate it, then set its DataContext to be the object whose properties you want to bind UI elements to:
var vm = new Class2(context.Patient);

var ui = new MyUI();

ui.DataContext = vm;
Now the binding to FullName should work.

Hope that helps,

Pete
Jul 11, 2014 at 3:36 PM
I've already done that in the main script which is as follows:

Public void Execute (ScriptContext context, System.Windows.Window window)
{
var vm = new Class2(context.Patient);
var UserControl1 = new UserControl1();
UserControl1.DataContext = vm;

window.Content = UserControl1;
}
Coordinator
Jul 11, 2014 at 5:19 PM
Can I see your XAML code. The view model injection looks correct. Instead of patient.Name, maybe try to set the property FullName to a string literal (just to make sure it has a value). Something like : FullName = "Fred Flinstone";
Jul 11, 2014 at 6:11 PM
The XAML code is below. I haven't been able to try the string literal that you suggested. I'll get on the Eclipse box later and mess around with it. I first binded it to the Text property but that didn't work so I binded it to the DataContext as well with no luck.

<StackPanel Margin="10" Height="322" DataContext="{Binding}" Width="157" VerticalAlignment="Top">
<TextBox Height="30" Name="txtPatientName" Width="150" Margin="3" DataContext ="{Binding FullName}" HorizontalAlignment="Left" Text="{Binding FullName}" />
<TextBox Height="30" Name="txtMRNumber" Width="150" Margin="3" DataContext="{Binding}" HorizontalAlignment="Left" />
<TextBox Height="30" Name="txtPlanName" Width="150" Margin="3" DataContext="{Binding}" HorizontalAlignment="Left" />
Coordinator
Jul 11, 2014 at 6:49 PM
Everything above the StackPanel in the XAML heirarchy should be clear of datacontext setters. You already (in the code) set the datacontext on the outermost object (the user control), it propagates downwards automatically.

Get rid of the binding on the datacontexts of the stackPanel and of the textbox. This should work.
<StackPanel Margin="10" Height="322" Width="157" VerticalAlignment="Top"> 
<TextBox Height="30" Name="txtPatientName" Width="150" Margin="3"  HorizontalAlignment="Left" Text="{Binding FullName}" /> 
<TextBox Height="30" Name="txtMRNumber" Width="150" Margin="3"  HorizontalAlignment="Left" />
<TextBox Height="30" Name="txtPlanName" Width="150" Margin="3"  HorizontalAlignment="Left" />
Marked as answer by rexcardan on 7/14/2014 at 8:30 AM
Jul 14, 2014 at 4:27 PM
Thanks Rex! It worked!