将DataContext绑定到UserControl.Resources ViewModel

时间:2016-05-30 08:46:55

标签: c# .net wpf mvvm data-binding

我想在UserControl.Resources中创建我的ViewModel,它应该从Control的DataContext设置属性内存 我将此添加到UserControl:

<UserControl.Resources>
    <vms:VMMemoryViewer Memory="{Binding Content}" x:Key="VMViewer"></vms:VMMemoryViewer>
</UserControl.Resources>

内存是DependencyProperty,定义如下:

public static readonly DependencyProperty MemoryProperty =
DependencyProperty.Register(nameof(Memory), typeof(IMemory), typeof(VMMemoryViewer),
        new FrameworkPropertyMetadata(null,new PropertyChangedCallback(OnMemoryChanged)));

Control的DataContext设置正确(我只是打印了一个带有DataContext和DataContext.Content绑定的标签)

但是,未设置内存属性 我在控制台中读到了错误:

  

System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:路径=内容;的DataItem = NULL; target元素是'VMMemoryViewer'(HashCode = 44436086); target属性是'Memory'(类型'IMemory')

但我不知道为什么......

0 个答案:

没有答案