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
Wpf 未通过数据绑定设置DependencyProperty值_Wpf_Data Binding_Binding_Dependency Properties - Fatal编程技术网

Wpf 未通过数据绑定设置DependencyProperty值

Wpf 未通过数据绑定设置DependencyProperty值,wpf,data-binding,binding,dependency-properties,Wpf,Data Binding,Binding,Dependency Properties,我有一个类,它有一个dependencProperty成员: public class SomeClass : FrameworkElement { public static readonly DependencyProperty SomeValueProperty = DependencyProperty.Register( "SomeValue", typeof(int), typeof(Som

我有一个类,它有一个
dependencProperty
成员:

public class SomeClass : FrameworkElement
{
    public static readonly DependencyProperty SomeValueProperty
        = DependencyProperty.Register(
            "SomeValue",
            typeof(int),
            typeof(SomeClass));
            new PropertyMetadata(
                new PropertyChangedCallback(OnSomeValuePropertyChanged)));

    public int SomeValue
    {
        get { return (int)GetValue(SomeValueProperty); }
        set { SetValue(SomeValueProperty, value); }
    }

    public int GetSomeValue()
    {
        // This is just a contrived example.
        // this.SomeValue always returns the default value for some reason,
        // not the current binding source value
        return this.SomeValue;
    }

    private static void OnSomeValuePropertyChanged(
        DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        // This method is supposed to be called when the SomeValue property
        // changes, but for some reason it is not
    }
}
该属性在XAML中绑定:

<local:SomeClass SomeValue="{Binding Path=SomeBinding, Mode=TwoWay}" />

当我访问
this.SomeValue
时,我没有获得绑定源的值。我做错了什么?

不幸的是,问题不在我共享的任何代码中。事实证明,我的
SomeClass
实例(我在
UserControl
中声明为资源)没有使用与
UserControl
相同的
DataContext
。我有这个:

<UserControl.Resources>
    <local:SomeClass x:Key="SomeClass" SomeValue="{Binding Path=SomeBinding, Mode=TwoWay}" />
</UserControl.Resources>


由于
SomeClass
对象没有正确的
DataContext
,因此未设置DependencyProperty…

我了解到WPF在运行时绕过了依赖项属性包装器,因此这可能解释了为什么在setter中未命中断点,但是我仍然不知道为什么我不能得到这个值…从阅读代码来看,这看起来没什么问题,这让我怀疑上下文是否设置得不正确。当您在调试器下运行它时,您是否在输出窗口中看到任何数据绑定错误,沿着“'SomeBinding'property not found on…”的线条?顺便说一句,请确认一下,您关于WPF绕过CLR wrapper属性的说法是正确的,这就是为什么setter中的断点不会被击中的原因。请参阅下面我的答案。我现在试图找出如何在ResourceDictionary中绑定资源的DataContext。。。为了这个我拼命地找。对于那些想知道的人,控件不会简单地从userControl继承datacontext。设置this.DataContext=\模型是不够的。还必须显式设置包含DependencyProperty的类的DataContext。this.DataContext=\u模型;SomeClass.DataContext=\u模型;好吧,在弄明白我在说什么几个小时后,我明白发生了什么。在自定义控件中,我设置了整个控件的DataContext。覆盖任何继承的DataContext。当我在控件内设置主网格的DataContext时,控件本身的DataContext继承得很好。哇,我太感谢你了,我花了几个小时试图弄清楚为什么会发生这种情况。我不知道我必须再次设置数据上下文。
<UserControl.Resources>
    <local:SomeClass x:Key="SomeClass" SomeValue="{Binding Path=SomeBinding, Mode=TwoWay}" />
</UserControl.Resources>