公开依赖属性我之前创建过

时间:2012-06-19 19:02:57

标签: c# wpf user-controls dependency-properties

我在WPF工作,我正在创建一些userControl,其中一些在其他人的内部。

在我的一个userControls后面的代码中,我创建了一个依赖属性,如下所示: (MyInnerControl.xaml.cs)

public static DependencyProperty MyDependencyProperty = DependencyProperty.Register(
        "MyProperty",
        typeof(Object),
        typeof(MyInnerControl));

public Object MyProperty
    {
        get
        {
            return (Object)GetValue(MyDependencyProperty );
        }
        set
        {
            SetValue(MyDependencyProperty , value);
        }
    }
...
...

这个属性完美无缺,但后来我尝试在另一个userControl中公开这个依赖,如下所示:

在我的容器userControl后面的代码中: (MyContainerControl.xaml.cs)

    public static DependencyProperty MyExternalDependencyProperty = DependencyProperty.Register(
        "MyExternalProperty",
        typeof(Object),
        typeof(MyContainerControl));

public Object MyExternalProperty
    {
        get
        {
            return (Object)GetValue(MyExternalDependencyProperty );
        }
        set
        {
            SetValue(MyExternalDependencyProperty , value);
        }
    }
...
...

在我的XAML中: (MyContainerControl.xaml)

<UserControl x:Class="MyControls.MyContainerControl" Name="this"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="329" d:DesignWidth="535" xmlns:my="clr-namespace:MyInnerControls">
<Grid>
    <my:MyInnerControl Margin="6,25,6,35" MyProperty="{Binding ElementName=this, Path=MyExternalProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
...
...

最后,我将MyContainerControl添加到网格并对我的viewModel进行绑定,如下所示: (AnotherContainer.xaml)

...
<MyContainerControl MyExternalProperty="{Binding MyExternalProperty}"/>
...

我的问题是我的ExternalProperty没有获取或设置任何值。我做错了什么?

为了澄清,我的DataContext已正确设置。

希望有人可以帮助我,提前谢谢你。

2 个答案:

答案 0 :(得分:1)

在我看来,问题是由于您没有遵循Dependency Property命名约定,即将依赖项属性名称设置为<ProperyName>Property(例如MyExternalProperty)和使用具有相同名称的CLR属性包装器,但省略“Property”后缀(在本例中为MyExternal)。遵循这个惯例应该解决你的问题。

来自MSDN - Dependency Properties Overview

  

属性的命名约定及其支持   DependencyProperty字段很重要。该字段的名称始终是   属性的名称,后缀为Property。更多   有关此约定的信息及其原因,请参阅自定义   依赖属性。

编辑:还有一些事情:

  1. 您的依赖项属性注册错误:您传递的是"MyProperty"而不是"MyDependency""MyExternalProperty"而不是"MyExternalDependency"
  2. 您应该避免使用this作为任何控件的名称,因为它是保留的关键字。

答案 1 :(得分:0)

您应该为UserControl提供x:Name并将该值用于ElementName

<UserControl x:Class="MyControls.MyContainerControl" Name="this"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     x:Name="ThisUserControl"
     d:DesignHeight="329" d:DesignWidth="535" xmlns:my="clr-namespace:MyInnerControls">
<Grid>
    <my:MyInnerControl Margin="6,25,6,35" MyProperty="{Binding ElementName=ThisUserControl, Path=MyExternalProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

... ...