![](/img/trans.png)
[英]Windows Phone 8 Pivot Control background transparent / translucent
[英]Biding with Pivot control on Windows Phone 8 with MVVM
我終於讓我的數據透視控件可以在wp8應用程序中使用MVVM進行工作了,但是我仍然有一個關於綁定的問題,因為它可以正常工作,我可以原樣接受它,我對結果不滿意,試圖了解為什么會這樣。 我的DataContext MainViewModel包含多個其他ViewModel。
方案1:
如果我在Grid(布局)中定義了DataContext,並且將數據透視表頭的itemsSource分配給QuickSearchTabs ViewModel,並且構建成功,但是我在數據透視表項中定義的列表框沒有被分配,則QuickSearchButtons ViewModel沒有被分配建立起來。 這是xaml代碼:
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource MainViewModel}" >
<phone:Pivot x:Name="Pivot" ItemsSource="{Binding QuickSearchTabs}" FontSize="{StaticResource PhoneFontSizeSmall}" SelectedIndex="{Binding SelectedSearchTabIndex, Mode=TwoWay}">
<phone:Pivot.Title>
<TextBlock Text="My Search Options" />
</phone:Pivot.Title>
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<ContentControl Content="{Binding Name}" />
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding QuickSearchButtons}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Content="{Binding Name}" Grid.Row="0">
</Button>
<TextBlock Text="{Binding Description}" Grid.Row="1">
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
</Grid>
方案2:
如果我在Grid(布局)中定義DataContext並在listbox標記中定義相同的DataContext,它將構建我的標頭和我的列表框,但是它將調用我的viewModel,該視圖模型被多次分配給列表框的ItemsSource。 確切地說,它將稱它為與我具有的樞軸數相同的時間。 這是xaml代碼:
<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{StaticResource CriteriaViewModel}" >
<phone:Pivot x:Name="Pivot" ItemsSource="{Binding QuickSearchTabs}" SelectedIndex="{Binding SelectedSearchTabIndex, Mode=TwoWay}" >
<phone:Pivot.Title>
<TextBlock Text="My Search Options" />
</phone:Pivot.Title>
<phone:Pivot.HeaderTemplate>
<DataTemplate>
<ContentControl Content="{Binding Name}"/>
</DataTemplate>
</phone:Pivot.HeaderTemplate>
<phone:Pivot.ItemTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding QuickSearchButtons}" DataContext="{StaticResource CriteriaViewModel}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Content="{Binding Name}" Grid.Row="0">
</Button>
<TextBlock Text="{Binding Description}" Grid.Row="1">
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</phone:Pivot.ItemTemplate>
</phone:Pivot>
</Grid>
如前所述,這是有效的,並且無論如何都不會影響我,因為始終會顯示正確的數據。
我可以以某種方式看到正在發生的事情,但是為什么在地球上為每個已定義的透視表頭設置ItemsSource。 當然,唯一重要的是可見性!
我不知道是否應該以我使用它們的方式使用樞軸。 到目前為止,看來,通常將視圖分配給每個PivotItem。 這不是我希望我的解決方案起作用的方式!
我只想要大量用於以特定方式對事物進行分組的標題,並且每個標題下顯示的內容都是動態生成的,但是在同一視圖上,即按鈕和標簽的列表。
關於如何使場景1)工作以及是否對場景2產生了任何想法,如何根據數據透視表頭項目的數量阻止它被觸發?
謝謝。
問題解決了!
當QuickSearchTabs應該是ViewModel的可觀察集合(即QuickSearchTabViewModel)時,它是QuickSearchTab的可觀察集合,並且在此viewModel中,它將為每個選項卡加載相關QuickSearchButton的可觀察集合。
擁有QuickSearchTabViewModel可以提供更大的靈活性,並且可以訪問當前選項卡(標題)以及其他相關屬性,包括在每個選項卡中維護的所有內容,例如按鈕。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.