绑定到代码隐藏抛出异常
Binding to code behind throws exception
我已经声明了一个命名空间如下(仅显示相关代码)
<Page
xmlns:local="using:ABC"
>
我的页面资源如下
<Page.Resources>
<local:Login x:Key="mykey"/>
</Page.Resources>
此处登录名是命名空间 ABC 内的 class,这个 class 是我的代码隐藏文件 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>
我的代码 class Login.xaml.cs 有两个 public 属性,分别称为 Mobile_Email 和密码。
当我在 class 构造函数中使用它时
mystackpanel.DataContext=this;
绑定有效,但像在页面资源中那样声明资源会出现 Whosebug 异常。
我的问题是为什么我会收到 Whosebug 异常以及如何将 xaml 中的堆栈面板与 class Login
后面的代码绑定
试试这个而不是 page.resources。为您的页面元素命名。然后将此绑定用于堆栈面板数据上下文。
{Binding ElementName=pagename,Path = DataContext}
我已经声明了一个命名空间如下(仅显示相关代码)
<Page
xmlns:local="using:ABC"
>
我的页面资源如下
<Page.Resources>
<local:Login x:Key="mykey"/>
</Page.Resources>
此处登录名是命名空间 ABC 内的 class,这个 class 是我的代码隐藏文件 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>
我的代码 class Login.xaml.cs 有两个 public 属性,分别称为 Mobile_Email 和密码。
当我在 class 构造函数中使用它时
mystackpanel.DataContext=this;
绑定有效,但像在页面资源中那样声明资源会出现 Whosebug 异常。
我的问题是为什么我会收到 Whosebug 异常以及如何将 xaml 中的堆栈面板与 class Login
后面的代码绑定试试这个而不是 page.resources。为您的页面元素命名。然后将此绑定用于堆栈面板数据上下文。
{Binding ElementName=pagename,Path = DataContext}