简体   繁体   English

绑定用户控件的数据上下文

[英]Binding datacontext of a Usercontrol

I'm starting in wpf app. 我从wpf应用程序开始。 And i'm trying to practice the MVVM patern. 而且我正在尝试练习MVVM模式。 I'm having trouble to bind the datacontext of a Usercontrol. 我在绑定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>

this is my usercontrol and Login is my class that i want to bind to my datacontext. 这是我的用户控件,而Login是我想绑定到我的datacontext的类。 I put this usercontrol in my PhonApplicationPage: 我把这个用户控件放在我的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>

And I'm going to finsih with my Login class: 我将用我的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();
    }
}
}

And I'm having the following error: Erreur 1 Cannot create instance of type 'Test.ViewModels.Login' [Line: 14 Position: 19] 我遇到以下错误:Erreur 1无法创建类型为'Test.ViewModels.Login'的实例[行:14位置:19]

Thanks for the help. 谢谢您的帮助。

您的班级应该是公开的时候是私人的

public class Login : MyNotifyPropertyChanged //add public keyword

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM