【发布时间】:2011-02-14 16:12:35
【问题描述】:
我想创建一个 ObservableCollection
目前我有:
internal static class Squiggle
{
public static readonly DependencyProperty NotificationsProperty = DependencyProperty.RegisterAttached(
"Notifications",
typeof(ObservableCollection<Notification>),
typeof(TextBox),
new FrameworkPropertyMetadata(null, NotificationsPropertyChanged, CoerceNotificationsPropertyValue));
public static void SetNotifications(TextBox textBox, ObservableCollection<Notification> value)
{
textBox.SetValue(NotificationsProperty, value);
}
public static ObservableCollection<Notification> GetNotifications(TextBox textBox)
{
return (ObservableCollection<Notification>)textBox.GetValue(NotificationsProperty);
}
...
}
使用以下 XAML:
<TextBox
x:Name="configTextBox"
Text="{Binding Path=ConfigText, UpdateSourceTrigger=PropertyChanged}"
AcceptsReturn="True"
AcceptsTab="True"
local:Squiggle.Notifications="{Binding Path=Notifications}"/>
不幸的是,当我实际运行它时,我得到一个异常说明:
“绑定”不能在“文本框”集合中使用。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。
这似乎只是在附加属性的类型为 ObservableCollection 时才会出现问题,因此 WPF 在绑定这种类型的属性时似乎正在尝试做一些神奇的事情,并在此过程中感到困惑。任何人都知道我需要做什么才能使其工作?
【问题讨论】:
标签: c# wpf data-binding