我有这个xaml代码:
<Common:LayoutAwarePage.Resources>
<CollectionViewSource x:Name="cvs" IsSourceGrouped="true" />
</Common:LayoutAwarePage.Resources>
<Grid>
<Full:TestSnapPage Name="MainView" />
...
从UserControl的代码隐藏中,我如何访问CollectionViewSource?
答案 0 :(得分:2)
将collectionviewsource
绑定到Tag并从后面的代码访问
< Full:TestSnapPage Name="MainView" Tag="{Binding Source={StaticResource cvs}}"/>
走到父页面,访问
背后的代码中的资源 var parentPage = GetParentsPage(this);
if (parentPage != null)
{
//parentPage.Resources["cvs"]
}
private ParentPage GetParentsPage(FrameworkElement @this)
{
FrameworkElement temp = @this;
while (temp.Parent != null)
{
if (temp.Parent is ParentPage)
return temp.Parent as ParentPage;
temp = temp.Parent as FrameworkElement;
}
return null;
}
使用MVVMLight框架进行视图之间(或视图模型之间)的通信。