【发布时间】:2014-01-14 22:45:46
【问题描述】:
,当我更新 observableCollection "MyCollection" 中的项目时,我希望我的自定义 TextBlock ( 执行函数并修改其文本。我想我应该调用函数 OnMYDataChanged:
<ListBox ItemsSource="{Binding MyCollection}" ItemTemplate="{StaticResource MyTemplate}" >
<DataTemplate x:Key="MyTemplate" >
<Grid >...
<local:MyTextBlock Path="{Binding MyText}" />
在哪里
public class MyTextBlock : TextBlock
{
public string Path
{ get {return (string)GetValue(PathProperty);}
set { SetValue(PathProperty, value); }
}
public static readonly DependencyProperty PathProperty =
DependencyProperty.Register("Path", typeof(string), typeof(MyTextBlock), new PropertyMetadata(OnMyDataChanged));
static void OnMyDataChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) {
Text = DoSomethingWithText(); //Does not work
}
当我更改一项时,会调用 OnMyDataChanged,但是 我在那里得到错误: 非静态字段、方法或属性需要对象引用
【问题讨论】:
标签: c# wpf inotifypropertychanged