【发布时间】:2014-09-30 11:07:24
【问题描述】:
我有一个树视图,我向其中添加了允许我绑定到当前选定项目的行为(我命名为 SelectedCourse 的依赖属性)。这似乎工作得很好,因为我可以暂停程序并确认所选项目的值正在改变:
这是 XAML 绑定:
<TextBox TextWrapping="Wrap"
Text="{Binding SelectedCourse.Description}">
</TextBox>
如您所见,当 SelectedCourse 更新时,UI 中什么也没有发生:
(右侧红色框为文本框,红色轮廓来自Snoop。)
BindableCourse 继承自 DependencyObject,Description 是一个依赖属性:
public class BindableCourse : DependencyObject
{
public static DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description",
typeof(string),
typeof(BindableCourse));
public string Description
{
get { return (string)GetValue(DescriptionProperty); }
set { SetValue(DescriptionProperty, value); }
}
}
现在,如果我查看 Snoop 中的文本框元素,SelectedCourse.Description 会根据需要出现在文本框中。
之后,我无法更改文本框;甚至通过,例如,更改 SelectedCourse - 我已经检查过 SelectedCourse 确实 在我选择新元素时发生更改 - 然后关闭 Snoop,重新打开它,然后重新检查文本框。
wtf 是怎么回事?
【问题讨论】:
-
TextBox.Text默认绑定两种方式,因此可能会更改所选课程的描述。尝试将Mode=OneWay添加到您的绑定中,看看是否有任何效果。假设您不希望用户能够更改描述,您还应该在TextBox上设置IsReadOnly="True"。
标签: c# wpf data-binding