0
votes

I am trying to implement a mvvm design pattern for xbap application But unable to carry out simple text binding.

Following is the definition of my DemoViewModel.cs,

class DemoViewModel : INotifyPropertyChanged { string name;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
            OnPropertyChanged("Name");
        }
    }

    public DemoViewModel()
    {
        Name = "test";
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }        
}

I am binding the view to viewmodel using code behind of view,

public DemoView() { InitializeComponent(); DataContext = new DemoViewModel(); }

Following is the binding definition for text box present in view,

2
xaml code for binding, <TextBox Margin="112,53,68,0" Name="textBox1" Height="23" VerticalAlignment="Top"> <TextBox.Text> <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged"/> </TextBox.Text> </TextBox> According to me 'test' should be displayedi ntext box upon execution which is not happneing. Please correct if i am wrong here - Mstechuser1
I have tested your code and it works perfectly. I am not sure what is going on. When you debug the application, check your output window, databinding errors are displayed in it. See if any of the messages in the window are of any help. - Agies
ya things are happening now as expected not sure y the text was not displayed during my first exec.. anyways thanks a lot - Mstechuser1

2 Answers

0
votes

I appears that you have everything hooked up correctly. During execution, take a look at you 'Output' window and see if it gives you any warnings on you Binding. Also, try to simplify your xaml a bit to the following and see if this helps:

<TextBox Text="{Binding Name, Mode=TwoWay}"/>
0
votes

Based on your comment, to JSPrang's answer, I know whats wrong =)

XBAP is missing permissions to use reflection, and can therefore only bind to public classes, unless run in full trust.