2013-07-11 32 views
3

我正在使用MVVM光,我想在我的視圖模型中將我的ListPicker連接到我的可觀察集合。我如何使我的ListPicker(從WP工具包)混合(MVVM光)

我能夠通過將它綁定到listPicker中的itemSource來完成此操作。我的問題是我想進入「FullScreenOnly」模式。我想我的datatemplate必須從itemSource繼承,因爲我可以將屬性名稱從我的集合中拉到數據模板中,並在運行應用程序時顯示它們。

但是,我看不到混合中的值。其中一個優點是「blendablity」,可以讓我在Expression Blend中看到虛擬值,以便更容易地使用。

視圖模型

public class AddProductPriceVm : ViewModelBase 
    { 
     /// <summary> 
     /// Initializes a new instance of the AddProductPrice class. 
     /// </summary> 
     public AddProductPriceVm() 
     { 
      if (ViewModelBase.IsInDesignModeStatic) 
      { 
       Stores = new ObservableCollection<Store> 
       { 
        new Store 
        { 
          Name = "test1", 
          Address = "123 fake street", 
          City = "Fake City", 
          Region = "FC", 
          PostalCode = "123414", 
        }, 
         new Store 
        { 
           Name = "test2", 
          Address = "123 fake street", 
          City = "Fake City", 
          Region = "FC", 
          PostalCode = "123414", 
        } 
       }; 
      } 
      else 
      { 
       Stores = new ObservableCollection<Store> 
       { 
        new Store 
        { 
          Name = "test1", 
          Address = "123 fake street", 
          City = "Fake City", 
          Region = "FC", 
          PostalCode = "123414", 
        }, 
         new Store 
        { 
           Name = "test2", 
          Address = "123 fake street", 
          City = "Fake City", 
          Region = "FC", 
          PostalCode = "123414", 
        } 
       }; 
      } 
      //Stores = new ObservableCollection<Store>(); 

     } 


     public ObservableCollection<Store> Stores { get; set; } 
    } 

所以在設計模式應該與我的虛擬數據填充,當它推出的直播應該填充我的虛擬數據。現在只有在實時模式下才會填充數據。

這裏是我有我的標記

<Grid.Resources> 
     <DataTemplate x:Name="PickerItemTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Name}" Margin="12 0 0 0"/> 
      </StackPanel> 
     </DataTemplate> 
     <DataTemplate x:Name="PickerFullModeItemTemplate"> 
      <Grid Height="129" Margin="16 21 0 20" Width="294" d:DesignWidth="198" d:DesignHeight="80"> 
       <TextBlock Text="{Binding Name}" FontSize="24" FontFamily="{StaticResource PhoneFontFamilyLight}" Height="34" VerticalAlignment="Top" TextWrapping="Wrap" Margin="0,0,8,0"/> 
       <TextBlock Text="Address: " HorizontalAlignment="Left" Margin="0,38,0,0" Width="92" Height="31" VerticalAlignment="Top"/> 
       <TextBlock Foreground="Green" TextWrapping="Wrap" Margin="0,69,0,0" Height="54" VerticalAlignment="Top" FontSize="18.667" Text="{Binding FullAddress}"/> 
       <Line x:Name="lineDivider" Stroke="White" RenderTransformOrigin="0.488,0.437" VerticalAlignment="Bottom" X2="500" StrokeThickness="3" Opacity="0.5" /> 
      </Grid> 
     </DataTemplate> 
    </Grid.Resources> 




    <toolkit:ListPicker ExpansionMode="FullScreenOnly" x:Name="lpStores" ItemTemplate="{StaticResource PickerItemTemplate}" FullModeItemTemplate="{StaticResource PickerFullModeItemTemplate}" Header="Cities" FullModeHeader="Cities" CacheMode="BitmapCache" Margin="22,173,35,0" Height="101" VerticalAlignment="Top" ItemsSource="{Binding Stores, Mode=TwoWay}"/> 

就像我說的在現場模式這項工作,但不融合。 enter image description here

上面是混合表達式4,正如你所看到的,我看到了我的虛擬值(我想要的)。

然而,當我試圖在Blend的數據進行編輯模板

enter image description here

什麼讓我到這裏

enter image description here

我只看到我的硬編碼值,而不是其他。

在我身邊,我看到3個文本塊和一個行分隔符。如果我看,我發現他們正確的數據綁定。

他們只是不顯示。如果我想讓它顯示了,我不得不再次進行數據綁定他們,但它使在這樣

Text="{Binding Stores[0].Name} 

然後我得到blendablity但住在都將無法正常工作(有什麼樣的怪),因爲它是硬綁定編碼到我的收藏的第一個索引。

回答

1

我的猜測是設計師在編輯完整模式項目模板時會失去數據上下文。但是,我可以通過在數據模板中的Grid上添加d:DataContext="{Binding Stores[0]}"聲明來實現混合。順便說一下,第三個TextBlock綁定被拼寫爲FullAddress,而該模型只有Address。總而言之,它看起來通過修改模板以下(省略不相關部分)的罰款:

<DataTemplate x:Name="PickerFullModeItemTemplate"> 
    <Grid Height="129" Margin="16 21 0 20" Width="294" d:DesignWidth="198" d:DesignHeight="80" d:DataContext="{Binding Stores[0]}"> 
     ... 
     <TextBlock Foreground="Green" TextWrapping="Wrap" Margin="0,69,0,0" Height="54" VerticalAlignment="Top" FontSize="18.667" Text="{Binding Address}"/> 
     ... 
    </Grid> 
</DataTemplate> 

在WP8.0項目測試與VS &混合2012年,工作正常,我。

+0

這似乎工作。我有點驚訝(但我又是數據綁定的新手)。我想現在網格中的所有孩子只能訪問第一個「商店」,而不能訪問任何其他商店。 – chobo2

+0

@ chobo2由於'd:'前綴,我們現在只爲網格設置設計時數據上下文。 'd:'前綴與Blend相關,並被標記爲'mc:Ignorable',這導致它在運行期間被_ignored_。所以我們不會在這裏搞亂你的運行時行爲,只是幫助設計者找到數據上下文。欲瞭解更多信息,請參閱[鏈接](http://blogs.msdn.com/b/mcsuksoldev/archive/2010/08/27/designdata-mvvm-support-in-blend-vs2010-and-wpf-silverlight.aspx) – htuomola

+0

啊很酷忘了「d:」。 – chobo2