UserControl无法正常工作

时间:2016-09-08 22:46:37

标签: xaml mvvm user-controls template10

这是我的usercontrol XAML

<UserControl x:Class="UserControlTest.Controls.FullNameControl"
             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:local="using:UserControlTest.Controls"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             d:DesignHeight="100"
             d:DesignWidth="100"
             mc:Ignorable="d">

    <Grid>
        <TextBlock Text="{Binding FirstNameText}" />
    </Grid>
</UserControl>

以下是usercontrol背后的代码

public sealed partial class FullNameControl : UserControl
{
    public FullNameControl()
    {
        this.InitializeComponent();
    }

    public string FirstNameText
    {
        get { return (string)GetValue(FirstNameTextProperty); }
        set { SetValue(FirstNameTextProperty, value); }
    }

    public static readonly DependencyProperty FirstNameTextProperty =
             DependencyProperty.Register("FirstNameText", typeof(string),
                typeof(FullNameControl), new PropertyMetadata(String.Empty));

}

以下是使用usercontrol的页面

<Page x:Class="UserControlTest.Views.PageWithUserControl"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:Behaviors="using:Template10.Behaviors"
      xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
      xmlns:Interactivity="using:Microsoft.Xaml.Interactivity"
      xmlns:controls="using:Template10.Controls"
      xmlns:uControls="using:UserControlTest.Controls"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:UserControlTest.Views"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:vm="using:UserControlTest.ViewModels"
      mc:Ignorable="d">

    <Page.DataContext>
        <vm:PageWithUserControlViewModel x:Name="ViewModel" />
    </Page.DataContext>

    <Grid>
        <uControls:FullNameControl FirstNameText="{Binding FirstName, Mode=OneWay}" />
    </Grid>
</Page>

这是填充视图的视图模型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Template10.Common;
using Template10.Mvvm;
using Template10.Services.NavigationService;
using Windows.UI.Xaml.Navigation;

namespace UserControlTest.ViewModels
{
    public class PageWithUserControlViewModel : ViewModelBase
    {
        public PageWithUserControlViewModel()
        {
        }

        private string _FirstName = "Default";
        public string FirstName { get { return _FirstName; } set { Set(ref _FirstName, value); } }

        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
        {

            FirstName = "Terrence";
            await Task.CompletedTask;
        }

        public override async Task OnNavigatedFromAsync(IDictionary<string, object> suspensionState, bool suspending)
        {
            await Task.CompletedTask;
        }

        public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
        {
            args.Cancel = false;
            await Task.CompletedTask;
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

所以缺少的部分是在InitializeComponent调用之后将这个datacontext赋值代码添加到用户控件的构造函数中

public FullNameControl()
{
    this.InitializeComponent();
    (this.Content as FrameworkElement).DataContext = this;
}