当UserControl具有DataContext时,UserControl的DependencyProperty为null

时间:2014-12-17 13:32:34

标签: c# wpf xaml mvvm

我在我的View中添加了一个DependencyProperty,绑定到DependencyProperty工作,但前提是我还没有设置DataContext。

GenericView.xaml

<UserControl x:Class="GenericProject.View.GenericView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
        <TextBox IsEnabled="False" Text="{Binding SomeProperty, Mode=OneWay}" />
    </StackPanel>
</UserControl>

GenericView.xaml.cs

public partial class GenericView : UserControl
{
    // The DependencyProperty for VMFactory.
    public static readonly DependencyProperty VMFactoryProperty = DependencyProperty.Register("VMFactory", typeof(VMFactoryViewModel<GenericViewModel>), typeof(GenericView));

    public VMFactoryViewModel<GenericViewModel> VMFactory
    {
        get { return (VMFactoryViewModel<GenericViewModel>)GetValue(VMFactoryProperty); }
        set { SetValue(VMFactoryProperty, value); }
    }

    public GenericView()
    {
        InitializeComponent();
    }
}

我在这里创建两个视图来说明手头的问题。第一个视图中的VMFactory绑定将失败,因为我已设置DataContext。第二种观点会成功,这种行为的原因是什么?

MainPage.xaml中

<vw:GenericView DataContext="{Binding Generic}" VMFactory="{Binding GenericFactory}" />
<vw:GenericView VMFactory="{Binding GenericFactory}" />

3 个答案:

答案 0 :(得分:4)

这是一个相当常见的Binding“gotcha”......

要访问VMFactory,您需要使用...将UserControl绑定到自身

DataContext="{Binding RelativeSource={RelativeSource Self}}"

然后,您不会将DataContext项目上的GenericView绑定到其他地方。

但是,如果您打算将其他值绑定到VMFactory外部的UserControl(即<vw:GenericView VMFactory={Binding ...}"/>),则应使用RelativeSource模式FindAncestor }对于UserControl类型。

<!-- Shortened to show pertinent Binding -->
<ctrl:CommandTextBox Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}"/>

答案 1 :(得分:0)

正如@toadflakz所说,这是WPF中一个非常常见的问题,而且当我学习WPF时,我花了一些时间来解决这个问题。幸运的是,解决方案很简单。我们假设我们有一个UserControl,其对象设置为DataContext,另一个设置为在DependencyProperty内声明的UserControl的值......你的情况。

UserControl XAML中,您可以像往常一样将数据绑定到对象的属性DataContext

<TextBlock Text="{Binding PropertyFromDataContextObject}" />

如果要将数据绑定到对象集中的对象作为DependencyProperty的值,则只需使用RelativeSource Binding

<TextBlock Text="{Binding PropertyFromDependencyPropertyObject, RelativeSource={
    RelativeSource AncestorType={x:Type YourPrefix:YourUserControl}}}" />

请注意,只要设置了BindingUserControl属性,这两个DataContext都可以在同一个DependencyProperty中一起使用。

答案 2 :(得分:0)

我有一个可行的解决方案,似乎控件的属性是相对于控件的DataContext绑定的?

我当然知道控件中的项目将相对于DataContext绑定,但我显然以前从未使用过这种方式的控件,并且不明白控件的属性也会继承set DataContext的范围。基本上我的视图中的所有内容都是正确的,但是对我的DependencyProperty的绑定失败了。

<强> GenericView.xaml

<!-- Everything in here was correct. -->
<UserControl x:Class="GenericProject.View.GenericView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel>
        <Button Command="{Binding VMFactory.CreateViewModelCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
        <TextBox IsEnabled="False" Text="{Binding SomeProperty, Mode=OneWay}" />
    </StackPanel>
</UserControl>

<强> MainPage.xaml中

<!-- This is where I messed up earlier, VMFactory requires a relative binding. -->
<vw:GenericView DataContext="{Binding Generic}"
                VMFactory="{Binding DataContext.GenericFactory, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" />