Mohit Shah
Mohit Shah

Reputation: 862

Binding to code behind throws exception

I have declared a namespace as follows (showing only relevant code)

<Page 
     xmlns:local="using:ABC"
 >

My page resources are as follows

 <Page.Resources>
    <local:Login x:Key="mykey"/>
 </Page.Resources>

Here Login is the class inside namespace ABC and this class is my code-behind file called Login.xaml.cs

        <StackPanel x:Name="mystackpanel" DataContext="{StaticResource mykey}" >

            <TextBox PlaceholderText="Email or Mobile Number" Text="{Binding Mobile_Email, Mode=TwoWay}"/>

            <TextBox PlaceholderText="Password" Text="{Binding Password, Mode=TwoWay}"/>

            <Button Content="Login" Click="LoginButton_Click"/>

        </StackPanel>

And My code behind class Login.xaml.cs has two public properties called Mobile_Email and Password.

When I use this in the class constructor

mystackpanel.DataContext=this;

The binding works but declaring the resource like that in page resources gives me stackoverflow exception.

My question is why I am getting a stackoverflow exception and how do I bind my stackpanel in xaml with the code behind class Login

Upvotes: 1

Views: 44

Answers (1)

Archana
Archana

Reputation: 3221

Try this instead of page.resources. Give a name to your page element. Then use this binding for stackpanel datacontext.

 {Binding ElementName=pagename,Path = DataContext}

Upvotes: 1

Related Questions