[英]Binding datacontext of a Usercontrol
我从wpf应用程序开始。 而且我正在尝试练习MVVM模式。 我在绑定Usercontrol的datacontext时遇到麻烦。
<UserControl x:Class="Test.Views.Login.Identifer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Test.ViewModels"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">
<UserControl.DataContext>
<vm:Login/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<TextBox Name="user"></TextBox>
<TextBox Grid.Row="1" Name="password"></TextBox>
</Grid>
这是我的用户控件,而Login是我想绑定到我的datacontext的类。 我把这个用户控件放在我的PhonApplicationPage中:
<phone:PhoneApplicationPage
x:Class="Test.Views.Login.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Views="clr-namespace:Test.Views.Login"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot est la grille racine où tout le contenu de la page est placé-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Views:Identifer></Views:Identifer>
</Grid>
我将用我的Login类结束:
using Test.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test.ViewModels
{
class Login : MyNotifyPropertyChanged
{
public Client client_ { get; set; }
public Login()
{
client_ = new Client();
}
}
}
我遇到以下错误:Erreur 1无法创建类型为'Test.ViewModels.Login'的实例[行:14位置:19]
谢谢您的帮助。
您的班级应该是公开的时候是私人的
public class Login : MyNotifyPropertyChanged //add public keyword
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.