【问题标题】:UserControl not working用户控件不起作用
【发布时间】:2017-01-16 23:54:09
【问题描述】:

这是我的用户控件 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>

这是用户控件的代码

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

}

这里是使用用户控件的页面

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

【问题讨论】:

  • “UserControl 不起作用”是什么意思?
  • 数据未从视图模型中填充。因此,具有该用户控件的所有页面都不会显示用户控件中这些字段的任何数据。

标签: xaml mvvm user-controls template10


【解决方案1】:

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

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

【讨论】:

  • 永远不要这样做,这是一个糟糕的习惯。在您的用户控件定义中,只需将 x:Name 添加到 元素。类似“WhateverNameThisIs_root”的东西。然后在您的绑定中,使用ElementName=WhateverNameThisIs_root。这就是你所要做的。
  • 1.为什么这是一个可怕的习惯?笼统地告诉 UC 它的 datacontext 是什么似乎是一件很正常的事情。
  • 2.我尝试了您的建议,但仍未填充数据。请记住,我正在绑定到不知道用户控件或其 x:name 的视图模型。我可能误会了。
  • 这很糟糕,因为它破坏了数据在 UI 中的流动方式。这样做的人经常发现自己必须创建一个伪 DataContext 属性,并将其绑定到他们的视图模型。我不知道你在 2. 中的问题是什么。您给 UserControl 的根目录一个 x:Name,然后在绑定中指定 ElementName,您可以在此处绑定到 UserControl 中定义的 DP stackoverflow.com/questions/18461660/…
猜你喜欢
  • 2019-09-18
  • 2012-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2014-12-07
  • 1970-01-01
相关资源
最近更新 更多