**下午好!**情況,collectionView里面還有一個collectionView。從代碼中我通過一個屬性得到第一個collectionView的方法和屬性
x:Name="cv1"
但是無法從代碼中訪問第二個集合。錯誤是
The name "cv2" does not exist in the current context.
我的 XAML:
<CollectionView x:Name="cv1">
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame Margin="5">
<Frame.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped"/>
</Frame.GestureRecognizers>
<StackLayout>
<Label Text="{Binding Key}"/>
<CollectionView x:Name="cv2">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Horizontal" HorizontalOptions="FillAndExpand">
<Label Text="{Binding DateCase}" HorizontalOptions="Start"/>
<Label Text="{Binding NumCase}" HorizontalOptions="CenterAndExpand"/>
<Label Text="{Binding Serial}" HorizontalOptions="CenterAndExpand"/>
<Label Text="{Binding Time}" HorizontalOptions="End"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
以下是代碼:
cv1.ItemsSource = Cases; //There is access to the collection
cv2.ItemsSource = TimeLine; //There is no access to the collection
cv2.IsVisible = flag;
告訴我出了什么事?用什么方法挖?謝謝。
預期:通過單擊包含 Collectionview 的 Frame,isVisible 屬性更改了它的值。
結果是:無法訪問 Frame 內的 collectionView,而后者又不在 collectionView 內。
uj5u.com熱心網友回復:
當然你不能從代碼中訪問 c??v2。名稱 CV2 是資料模板的一部分,而不是您的頁面。
您實際要問的是:如何在 DataTemplate 中訪問控制。
每次插入 CV1 專案時都會生成新的 CV2。您需要在當前專案的背景關系中搜索它。(如果你需要使用這種方法)
你實際上需要做的是:
- 使用系結。
- 為您的 DataTemplate(CV1 項)設定 x:DataType。
- 將您的 CV2 ItemSource 系結到集合 x:DataType 模型的串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/524081.html
標籤:Cxml毛伊岛
下一篇:MVVM:如果我設定DefaultStyleKeyProperty,自定義TreeViewItem不會顯示在TreeView中
