Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF附加属性和绑定的奇怪行为_C#_Wpf_Xaml - Fatal编程技术网

C# WPF附加属性和绑定的奇怪行为

C# WPF附加属性和绑定的奇怪行为,c#,wpf,xaml,C#,Wpf,Xaml,我试图在网格控件上注册WPF附加属性,但是,今天我遇到了非常奇怪的行为: public static class MyClass { public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.RegisterAttached("MyProperty", typeof(string), typeof(MyClass), null); publi

我试图在网格控件上注册WPF附加属性,但是,今天我遇到了非常奇怪的行为:

public static class MyClass
{
    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.RegisterAttached("MyProperty", typeof(string),
        typeof(MyClass), null);

    public static string GetMyProperty(DependencyObject d)
    {
        return (string)d.GetValue(MyPropertyProperty);
    }

    public static void SetMyProperty(DependencyObject d, string value)
    {
        d.SetValue(MyPropertyProperty, value); //<-- set breakpoint here
    }
}
i、 e.使用MyProperty以外的其他名称。然后可以命中断点!并且可以设置值

此外,当我将所附属性更改为:

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.RegisterAttached("MyProperty", typeof(string),
        typeof(UIElement), null);
i、 e.将所有者类型更改为UIElement,然后我也可以点击断点,只是想知道为什么

但是,当我在XAML中设置绑定而不是字符串常量时,上面的每种情况都会出现一个异常,即
只能在DependencyObject的DependencyProperty上设置“binding”

绑定XAML示例:

<GridControl local:MyClass.MyProperty="{Binding MyStringValue}">
...
</GridControl>

...

以前有人见过这种奇怪的行为吗?我的案子里遗漏了什么?提前感谢您的回复

如果您将
SetMyProperty
方法称为“setter”,那么您应该知道这些方法只是供您使用的“helper”方法。该框架通常不使用这些方法

但是,如果您说希望知道值何时更改,那么还有另一种方法。将
PropertyChangedCallback
处理程序添加到属性声明中:

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached("MyProperty", typeof(string), typeof(MyClass), 
    new UIPropertyMetadata(default(string.Empty), OnMyPropertyChanged));

public static void OnMyPropertyChanged(DependencyObject dependencyObject, 
    DependencyPropertyChangedEventArgs e)
{
    string myPropertyValue = e.NewValue as string;
}

如果您将
SetMyProperty
方法称为“setter”,那么您应该知道这些方法只是供您使用的“helper”方法。该框架通常不使用这些方法

但是,如果您说希望知道值何时更改,那么还有另一种方法。将
PropertyChangedCallback
处理程序添加到属性声明中:

public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached("MyProperty", typeof(string), typeof(MyClass), 
    new UIPropertyMetadata(default(string.Empty), OnMyPropertyChanged));

public static void OnMyPropertyChanged(DependencyObject dependencyObject, 
    DependencyPropertyChangedEventArgs e)
{
    string myPropertyValue = e.NewValue as string;
}

谢谢谢里登的回答,这就是我需要的!很抱歉,回复太晚了。谢谢谢里登的回答,这正是我需要的!很抱歉,我没有及时回复。
public static readonly DependencyProperty MyPropertyProperty =
    DependencyProperty.RegisterAttached("MyProperty", typeof(string), typeof(MyClass), 
    new UIPropertyMetadata(default(string.Empty), OnMyPropertyChanged));

public static void OnMyPropertyChanged(DependencyObject dependencyObject, 
    DependencyPropertyChangedEventArgs e)
{
    string myPropertyValue = e.NewValue as string;
}