【发布时间】:2012-02-09 14:57:30
【问题描述】:
我的以下代码运行良好:
<Viewbox.Resources>
<CollectionViewSource x:Key="viewSource"
Source="{Binding Path=SelectionList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Viewbox.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource ResourceKey=viewSource}}"/>
我想将我的 CollectionViewSource 直接放在我的 ComboBox 中,而不使用任何类似的资源:
<ComboBox SelectedItem="{Binding Path=Value, Mode=TwoWay}">
<ComboBox.ItemsSource>
<Binding>
<Binding.Source>
<CollectionViewSource Source="{Binding Path=SelectionList}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Description" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Binding.Source>
</Binding>
</ComboBox.ItemsSource>
</ComboBox>
但是这样我的 ComboBox 总是空的,我得到以下绑定错误:
System.Windows.Data 错误:2:找不到管理 FrameworkElement 或 FrameworkContentElement 为目标元素。 绑定表达式:路径=选择列表;数据项=空;目标元素是 'CollectionViewSource' (HashCode=1374711);目标属性是“源” (类型“对象”)
有谁知道我该怎么做?
【问题讨论】:
-
这只是一个猜测,但您可能必须显式设置 CollectionViewSource.Source 绑定的 Source:
<CollectionViewSource Source="{Binding Source=... Path=SelectionList}"。您是否看到任何绑定错误消息? -
感谢您的评论,我检查了我的装订错误。根据您的建议,我可以在 Source=... 中添加什么
-
将其设置为拥有 SelectionList 属性的对象。
-
SelectionList 在 ViewModel/DataContext 中定义。我试过
但它不起作用。 -
@Nicolas 明确地说,我认为他是指非绑定源,例如
{RelativeSource Self},然后绑定到DataContext.SelectionList属性
标签: wpf