【问题标题】:Binding the DataContext of a UserControl in XAML is not working (Windows Phone)在 XAML 中绑定 UserControl 的 DataContext 不起作用(Windows Phone)
【发布时间】:2012-10-05 21:49:22
【问题描述】:

当我绑定UserControlDataContext 时,绑定工作正常。当我调试程序时,我可以看到UserControlDataContext 绑定到正确的ViewModel。但是,当我将UserControl 中的特定属性(如文本框)绑定到ViewModel 时,出现故障。 UserControl 将正确获取 ViewModel 的初始值。如果我有一些默认值为“test”的绑定字符串属性,UserControl 会正确显示。但是,没有任何更改会传播回ViewModel。因此,在最初显示“test”的文本框中输入“abcd”不会将ViewModel 更新为“abcd”。 ViewModel 错误地包含“测试”字符串。

如果上述内容不清楚或不够具体,请继续。我将使用代码和 XAML sn-ps 来布置我的架构。

我有一个聚合其他 VM 的 VM。在此父虚拟机中,子虚拟机作为可绑定属性公开。

public class ParentViewModel : ViewModel
{
    #region Binding Properties

    private const string ChildViewModelPropertyName = "ChildViewModel";
    private ChildViewModel childViewModel = new ChildViewModel();
    public ChildViewModel ChildViewModel 
    {
        get { return this.childViewModel ; } 
        set
        {
            if (value == this.childViewModel )
            {
                return;
            }
            this.childViewModel = value;
            RaisePropertyChanged(ChildViewModelPropertyName);
        }
    }

    //... 
}

ChildViewModel 是您的典型 VM,它公开了一些其他属性,例如我想绑定到文本框或不绑定到文本框的 Strings

public class ChildViewModel : ViewModel
{
    #region Binding Properties

    private const string NamePropertyName = "Name";
    private string name = "";
    public string Name
    {
        get { return this.name; }
        set
        {
            if (value == this.name)
            {
                return;
            }
            this.name = value;
            RaisePropertyChanged(NamePropertyName);
        }
    }

    //...
}

在 XAML 上。第一个 XAML sn-p 表示整个页面,并且此页面的 DataContext 绑定到 ParentViewModel。此页面包含一个UserControl,它有一个DataContext 绑定,该绑定与ParentViewModel 中公开的ChildViewModel 属性相关联。

<phone:PhoneApplicationPage ...>

   <!-- Sets the DataContext for the page. -->
   <phone:PhoneApplicationPage.DataContext>
        <vm:ParentViewModel/>
   </phone:PhoneApplicationPage.DataContext>

   <ScrollViewer>
       <phone:Pivot>
           <phone:PivotItem>
               <!-- ChildView is the UserControl whose DataContext should be bound to ChildViewModel -->
               <view:ChildView DataContext="{Binding ChildViewModel}"/>
           </phone:PivotItem>
       </phone:Pivot>
   </ScrollViewer>

</phone:PhoneApplicationPage>

ChildView XAML 非常简单。在ChildViewModel 中有一个绑定到Name 属性的文本框。明确一点,我没有在ChildView中绑定任何DataContext,因为它已经绑定在上面的sn-p中了。

<UserControl ... >

    <StackPanel>
        <!-- Binding to the Name property -->
        <TextBox Text="{Binding Name}"/>
    </StackPanel>

</UserControl> 

如果有人能告诉我为什么这种方法不起作用,我会支持你的回答并说“谢谢!”。

【问题讨论】:

  • 运行时在输出窗口中可以看到什么?是不是说有一些绑定错误?
  • 没有错误报告。只是一些线程退出和 dll 正在加载的消息。
  • 我试图重现该问题但没有成功。一切对我来说都很好。很难说出您的应用程序出了什么问题。你确定你从来没有在代码隐藏中设置 DataContext 吗?
  • 感谢您尝试复制。以上是实际编码的简化版本。我将再次浏览代码。我一定是错过了什么!
  • 我做了更多的调试,现在问题对我来说更清楚了。我将重申我认为问题出在原始问题中的内容,并包含更多关于子视图的示例 XAML。

标签: c# silverlight windows-phone-7 mvvm


【解决方案1】:
<TextBox Text ="{Binding Name,
                Mode=TwoWay}"/>

将绑定模式设置为TwoWay 将让View 更新ViewModel

【讨论】:

  • 噢!我认为双向模式是默认的。唉,事实并非如此。
猜你喜欢
  • 2014-06-25
  • 2013-04-06
  • 2012-12-09
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 2018-09-24
相关资源
最近更新 更多