WPF依赖项属性 - 被忽略

时间:2014-02-09 16:09:32

标签: c# wpf

我有以下课程:

public class Person:DependencyObject
    {
        public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(String), typeof(Person));

        public string Name
        {
            get
            {
                string result = (string)GetValue(NameProperty);
                return result;
            }
            set
            {
                SetValue(NameProperty, value);
            }
        }
    }  

以下窗口:

<Window x:Class="BindingSelf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" Text="{Binding Name}"></TextBox>
    </Grid>
</Window>  

Window背后的代码是:

public partial class MainWindow : Window
    {
        Person p = null;
        public MainWindow()
        {
            InitializeComponent();

            p = new Person();
            p.Name = "Test1";
            this.DataContext = p;
        }

    }

TextBox绑定到Name,并在运行应用程序时正确显示它的值(“Test1”)。现在这是我的问题,如果我在Name属性的Get部分设置了一个断点,它将被完全忽略。我已经完成了一些测试,即使如果我返回空“Test1”仍然显示,有人可以解释发生了什么吗?

由于

1 个答案:

答案 0 :(得分:1)

来自MSDN链接的解释是不言自明的:

  

其XAML处理器的当前WPF实现本质上是   依赖属性意识到。 WPF XAML处理器使用属性系统   加载二进制XAML和时的依赖属性的方法   处理作为依赖项属性的属性。这有效   绕过属性包装器。实现自定义依赖项时   属性,你必须考虑到这种行为,应该避免   将任何其他代码放在属性包装器中除了   属性系统方法GetValue和SetValue。