2013-03-30 39 views
1

我終於得到了我的數據透視控制在wp8應用程序中使用MVVM的工作,但我仍然有一個關於綁定的問題,因爲它起作用,我可以接受,我對結果不滿意,我試圖理解爲什麼會發生這種情況。我的DataContext,MainViewModel包含多個其他ViewModel。在Windows Phone 8上使用MVVM與數據透視控件進行綁定

方案1:

如果我定義的DataContext在網格(佈局),我分配的ItemsSource爲樞軸頭到QuickSearchTabs視圖模型,這得到建行,但列表框我已經內pivotitem沒有按規定沒有建立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:

如果我定義的網格(佈局)在DataContext和列表框標籤中定義的相同的DataContext,它會建立我的頭,我的列表框BUT它會多次調用我分配給列表框的ItemsSource的viewModel。確切地說,它會將其稱爲與我擁有的樞軸數相同的時間。這裏是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。當然,唯一重要的就是能見度的人!

我不知道樞軸是否會按照我使用它們的方式使用。看來,從我目前看到的情況來看,通常每個數據透視項都分配了一個視圖。這不是我想要我的解決方案工作的方式!

我只是想要用於以特定方式對事物進行分組的許多標題,並且每個標題下顯示的內容都是動態構建的,但在相同的視圖上即按鈕和標籤列表上。

關於如何獲得場景1)的任何想法,以及如果我與場景2卡住了,如何根據數據透視標題項目來阻止它被觸發?

謝謝。

回答

1

問題解決了!

QuickSearchTabs是QuickSearchTab的可觀察集合,它應該是ViewModel的可觀察集合,即QuickSearchTabViewModel,並且在此視圖模型中,它將爲每個選項卡加載相關QuickSearchButton的可觀察集合。

擁有一個QuickSearchTabViewModel提供了更多的靈活性,它將允許訪問當前標籤(標題)以及其他相關屬性,包括每個標籤內的所有內容,比如我的按鈕。

希望這會有所幫助。