2015-10-26 74 views
0

快速问题,是否有可能绑定到具有编译绑定的资源键,就像使用传统绑定一样?编译绑定(x:绑定)到资源键

即 传统装订

<Page.Resources> 
    <local:DataSource x:Key="Data"/> 

    <CollectionViewSource x:Name="myColl" Source="{Binding Source={StaticResource Data}, Path=Colleges}" ItemsPath="Campuses" IsSourceGrouped="True"/> 
</Page.Resources> 

编译绑定

我尝试过了,我知道这是行不通的,显然,这是找物业“数据”页面,而不是资源上。

<Page.Resources> 
    <local:DataSource x:Key="Data"/> 
    <CollectionViewSource x:Name="myColl" Source="{x:Bind Data.Colleges}" ItemsPath="Campuses" IsSourceGrouped="True"/> 
</Page.Resources> 

那么还有其他方法吗?

回答

0

顾名思义,使用编译的绑定方式表示在编译时验证绑定表达式,因此必须强类型化。这意味着路径需要在上下文中作为属性存在,并且不会用作资源。

对于{X:}绑定工作,后面声明你的数据源作为一个属性代码:

public sealed partial class MainPage : Page 
{ 
    public DataSource Data { get; set; } 

    public MainPage() 
    { 
     InitializeComponent(); 
     Data = new DataSource(); 
    } 
} 

然后从你的资源删除您的数据资源,你就大功告成了。