【问题标题】:Binding datacontext of a Usercontrol绑定用户控件的数据上下文
【发布时间】:2015-02-04 02:17:49
【问题描述】:

我开始使用 wpf 应用程序。我正在尝试练习 MVVM 模式。我无法绑定用户控件的数据上下文。

<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>

这是我的用户控件,登录是我想绑定到我的数据上下文的类。我把这个用户控件放在我的 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>

我将结束我的登录类:

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();
    }
}
}

我遇到以下错误: 错误 1 ​​无法创建类型为“Test.ViewModels.Login”的实例 [行:14 位置:19]

感谢您的帮助。

【问题讨论】:

    标签: c# windows-phone-8 mvvm datacontext


    【解决方案1】:

    你的课程应该是公开的却是私有的

    public class Login : MyNotifyPropertyChanged //add public keyword
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-29
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多