我有一个类,其中包含一个 DependencyProperty
成员:
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}" />
我正在使用MVVM框架,因此我的viewmodel是此XAML文件的DataContext
。绑定源属性看起来像这样:
public int SomeBinding
{
get { return this.mSomeBinding; }
set
{
this.mSomeBinding = value;
OnPropertyChanged(new PropertyChangedEventArgs("SomeBinding"));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, e);
}
return;
}
当我访问this.SomeValue
时,我没有获得绑定源的值。我做错了什么?