Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 使用Binding.Source而不使用资源_Wpf - Fatal编程技术网

Wpf 使用Binding.Source而不使用资源

Wpf 使用Binding.Source而不使用资源,wpf,Wpf,我有以下代码运行良好: <Viewbox.Resources> <CollectionViewSource x:Key="viewSource" Source="{Binding Path=SelectionList}"> <CollectionViewSource.SortDescriptions> <scm:SortDescription Proper

我有以下代码运行良好:

<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 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>

但这样,我的组合框总是空的,我会得到以下绑定错误:

System.Windows.Data错误:2:找不到治理框架元素 或目标元素的FrameworkContentElement。 BindingExpression:Path=SelectionList;DataItem=null;目标元素是 “CollectionViewSource”(HashCode=1374711);目标属性为“源” (键入“对象”)


有人知道我该怎么做吗?

您会遇到错误,因为
CollectionViewSource
没有父级继承要在绑定中使用的
DataContext

不过,组合框不需要使用
CollectionViewSource
。通过绑定
ItemsSource
属性,可以将其项源绑定到任何集合

<ComboBox ItemsSource="{Binding SelectionList}"
          SelectedItem="{Binding Path=Value, Mode=TwoWay}" />


唯一缺少的是排序,但是您可以在将数据返回到视图之前对ViewModel中的数据进行排序。

Nicolas,虽然这不是您问题的答案,因为它仍然使用资源,但您可以通过在其本地资源字典中定义CollectionViewSource将其放入组合框中:

<ComboBox>
    <ComboBox.Resources>
        <CollectionViewSource x:Key="viewSource" Source="{Binding Path=SelectionList}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="Description" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </ComboBox.Resources>
    <ComboBox.ItemsSource>
        <Binding Source="{StaticResource viewSource}"/>
    </ComboBox.ItemsSource>
</ComboBox>


这只是猜测,但您可能必须明确设置CollectionViewSource的源。源绑定:
感谢您的评论,我检查了绑定错误。在您的建议中,我可以在Source=…中将其设置为拥有SelectionList属性的对象。SelectionList在ViewModel/DataContext中定义。我试过了,但没有用。@Nicolas所说的显式,我想他指的是一个非绑定源,比如
{relativesourceself}
,然后绑定到
数据上下文。SelectionList
PropertyTanks以获取答案。我真的需要在我的视图中排序,而不是在我的ViewModel中,这就是我使用CollectionViewSource的原因。您能告诉我为什么它在资源中工作而不是在组合框中。@Nicolas它作为资源工作,因为
Binding.Source
属性指向的是已经在UI中定义的对象。另外,您不能对非
依赖属性
的属性使用绑定,我不认为
绑定。Source
依赖属性
。根据您的原始集合从ViewModel返回排序的
CollectionViewSource
属性没有错。@Rachel,这里设置的不是绑定的源,而是CollectionViewSource的源。@Clemens哦,对了,忘了:)在这种情况下,它不起作用,因为
CollectionViewSource
不是可视树或逻辑树的一部分,因此没有从中继承要在绑定中使用的DataContext的父级。更新了我的答案。@Rachel Right,这就是CollectionViewSource的原因。源的绑定需要显式的源设置,即指向拥有SelectionList属性的对象。@H.B.此答案已被显著编辑,注释引用了旧答案。旧的解决方案确实不起作用。总而言之,CollectionViewSource不知道DataContext,并且无法将DataContext提供给他,因为CollectionViewSource不在VisualTree中。因此,唯一的办法就是利用资源。克莱门斯和瑞秋,谢谢你的专业知识。