我正在构建一个可以编辑POCO的控件。有一个POCO内的字段需要编辑的描述符集合,我将这个集合绑定到ListBox
的ItemsSource
。除其他外,描述符使我能够选择合适的DataTemplate
以及该项目应该编辑的POCO中的变量名称。在XAML绑定表达式中使用变量
我的列表框是建立这样的:
<ListBox ItemsSource="{Binding ColumnCollection, ElementName=root}">
<ListBox.Resources>
<DataTemplate x:Key="TextTemplate">
<StackPanel>
<TextBlock Text="{Binding DisplayName}" />
<!-- !!! Question about following line !!! -->
<TextBox Text="{Binding ElementName=vm.CurentEditing, Path=PathName}" />
</StackPanel>
</DataTemplate>
<!-- Details omitted for brevity -->
<DataTemplate x:Key="PickListTemplate" />
<DataTemplate x:Key="BooleanTemplate" />
</ListBox.Resources>
<ListBox.ItemTemplateSelector>
<local:DataTypeSelector
TextTemplate="{StaticResource TextTemplate}"
PickListTemplate="{StaticResource PickListTemplate}"
BooleanTemplate="{StaticResource BooleanTemplate}"
/>
</ListBox.ItemTemplateSelector>
</ListBox>
这是在“TextTemplate”我是具有问题TextBox
绑定表达式。问题是不应将“PathName”视为文字字符串,而是ColumnDescription
类(用于ListBox.ItemsSource
的ColumnCollection
的集合类型)中的字符串属性的名称,该名称给出了我想要的POCO属性的名称绑定到(POCO是“vm.CurrentEditing”)。
是否有某种方法可以将XAML中的属性值用作绑定表达式的输入,还是必须使用后面的代码? (顺便说一下,如上所述,将ElementName
指定为“xy”似乎也是无效的。我假设“y”部分应该在Path
之内,但是目前我的属性名称已被占用......)
'ElementName'只是名称,其他所有内容都在'Path'中,使用'.'来访问子属性。 –
你想做类似于ListBox.DisplayMemberPath的事吗? –
@ H.B。我认为一定是这种情况(并且可能在过去的某个时候知道)。感谢您的确认。 –