如何更改CollectionView.Source的绑定? [英] How do I change the binding of a CollectionView.Source?

查看:137
本文介绍了如何更改CollectionView.Source的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我在XAML中有这个代码:

Initially I had this code in XAML:

<CollectionViewSource x:Name="cSource">
  <CollectionViewSource.Source>
    <Binding Source="{StaticResource NameOfXmlDataProvider}" XPath="letter"/>
  </CollectionViewSource.Source>
<CollectionViewSource>

但是我想在C#代码中保留绑定对象,以便能够动态更改它的xpath 。
目前我有这个代码:

But I wanted to keep a binding object in the C# code, to be able to dynamically alter it's xpath. Currently I have this code:

CollectionViewSource viewSource = this.FindResource("cSource") as CollectionViewSource;
Binding binding = new Binding( "Source" );
binding.Source = _xmlDataProvider;
binding.XPath = "/new/path/to/nodes";
BindingOperations.SetBinding( viewSource, CollectionViewSource.SourceProperty, binding );

这个编译并没有抱怨,但是调用它时只会导致一个空列表。
我似乎没有在网络中找到相关的例子 - 他们大多数都是处理数据提供者,但是我想改变绑定。

This compiles and doesn't complain but when called it only leads to an empty list. I can't seem to find related examples in the web - most of them deal with the data providers but I want to change the binding.


  • 任何人都知道如何解决这个问题?

  • 还是有更好的方法?

  • 也许从检索绑定对象

推荐答案

而不是绑定到XAML中的静态资源(yuk)或动态更改绑定(yukkier),绑定到您可以更改的内容。

Rather than bind to a static resource in the XAML (yuk) or dynamically change the binding (yukkier), bind to things you can change.

<CollectionViewSource x:Name="cSource">
  <CollectionViewSource.Source>
    <Binding Source="{Binding MyDataProviderProperty}" XPath="{Binding MyDataXPathProperty}"/>
  </CollectionViewSource.Source>
<CollectionViewSource>

如果你不想去完整的MVVM,一个很好的技巧是你可以绑定你的页面DataContext通过简单地命名页面的 UserControl 元素并使用 ElementName 绑定数据文本对它(唯一的限制是DataContext绑定不能也在UserControl上(所以把它放在第一个孩子,像一个网格):

If you don't want to go full MVVM, a nice trick is that you can bind your page DataContext to your page's code-behind class by simply naming the UserControl element of your page and use ElementName to bind the datacontext to it (the only restriction is that the DataContext binding can't also be on the UserControl (so place it on the first child, like a grid):

<UserControl x:Class="BindingTests.BindingToSelfExample"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" x:Name="MyViewClass">
    <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding ElementName=MyViewClass}">
        <TextBlock Text="{Binding SomePropertyOfTheCodeBehind}" Width="100" Height="20"/>
    </Grid>
</UserControl>

现在,只要你在MyDataProviderProperty和MyDataXPath后面的代码中通知属性,你可以更改他们对你心中的内容。

Now, so long as you have notify properties on your code behind called MyDataProviderProperty and MyDataXPath, you can change them to your heart's content.

这篇关于如何更改CollectionView.Source的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆