我有一个树视图,我已经添加了行为,允许我绑定到当前选定的项目(依赖属性我已经命名为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会根据需要显示在文本框中。
之后,我无法更改文本框;例如,当我选择一个新元素 - 然后关闭Snoop,重新打开它,然后重新检查文本框时,即使通过更改SelectedCourse - 我已检查并且SelectedCourse 也会被更改
wtf正在进行?
答案 0 :(得分:1)
我认为您可以执行以下操作,而不是使用自己的自定义绑定:
<TextBox TextWrapping="Wrap"
Text="{Binding ElementName=TreeViewName, Path=SelectedValue.Description}">
</TextBox>
所选值将根据树视图选择而改变。 “TreeViewName”是使用x:Name属性的树视图的名称。