2012-06-17 93 views
4

我想知道如何使用CollectionViewSource在位於SampleData.xaml中的Expression Blend中顯示設計時間數據?在更改我的代碼以使用CVS之前,我使用了一個ObservableCollection。我需要過濾和排序內部的項目,因此我更改了代碼以使用CVS。現在我的設計師抱怨無法使用適當的結構填充SampleData的NextItems以顯示在Expression Blend中。下面是一些代碼,我使用的內部應用程序:如何在Expression Blend中使用CollectionViewSource和設計時間數據?

MainViewModel.cs

class MainViewModel 
{ 
    public MainViewModel() 
    { 
     AllItems = new ObservableCollection<ItemViewModel>(); 
     NextItems = new CollectionViewSource(); 
     NextItems.Source = AllItems; 
    } 

    public CollectionViewSource NextItems 
    { 
     get; 
     private set; 
    } 

    public ObservableCollection<ItemViewModel> AllItems 
    { 
     get; 
     private set; 
    } 

    some functions to fill, filter, sort etc... 
} 

MainView.xaml:

<phone:PhoneApplicationPage 
    ... some other stuff ... 
    d:DesignWidth="480" 
    d:DesignHeight="728" 
    d:DataContext="{d:DesignData SampleData/SampleData.xaml}"> 

    <Grid 
     x:Name="LayoutRoot" 
     Background="Transparent"> 
     <controls:Panorama> 
      <controls:PanoramaItem> 
       <ListBox ItemsSource="{Binding NextItems.View}"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <Image Source="{Binding Image}" /> 
           <StackPanel> 
            <TextBlock Text="{Binding FullName}" /> 
           </StackPanel> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </controls:PanoramaItem> 
     </controls:Panorama> 
    </Grid> 
</phone:PhoneApplicationPage> 

SampleData.xaml

<local:MainViewModel 
    xmlns:local="clr-namespace:MyAppNamespace" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:swd="clr-namespace:System.Windows.Data;assembly=System.Windows" > 
    <local:MainViewModel.AllItems> 
     <local:ItemModel 
      FullName="Dummy" 
      Image="/Images/dummy.png" /> 
    </local:MainViewModel.AllItems> 

    <local:MainViewModel.NextItems> 

     How to fill the CollectionViewSource's Source? 

    </local:MainViewModel.NextItems> 
</local:MainViewModel> 

所以,問題我可以找不到答案是如何在SampleDate.xaml中填充NextItems的Source?任何幫助將非常感激。

回答

1

如果您想在設計器中顯示示例數據,我會建議您從代碼中執行此操作。有兩種爲Blend Designer或VStudio設計器生成樣本數據的方法:

  1. 從您的XML文件中進行操作。
  2. C#類 - >最好的選擇

最好的選擇。

在WPF中,在Windows 8和WP7.5和highger,您可以訪問一個名爲propertie:Windows.ApplicationModel.DesignMode.DesignModeEnabled利用它可以從您的視圖模型種子您的ObservableCollection:

public class MainViewModel 
{ 
    public MainViewModel() 
    { 
     AllItems = new ObservableCollection<ItemViewModel>(); 

     if (DesignMode.DesignModeEnabled) 
     { 
      AllItems = FakeDataProvider.FakeDataItems; 
     } 
     NextItems.Source = AllItems; 
    } 

    public CollectionViewSource NextItems 
    { 
     get; 
     private set; 
    } 

    public ObservableCollection<ItemViewModel> AllItems 
    { 
     get; 
     private set; 
    } 
} 

這樣,如果你改變了模型,你不需要重新生成一個XML文件,它比C#文件有點乾淨。 FakeDataProvider是一個靜態類,用於存儲所有設計時僞造的數據。因此,在您的XAML中,您唯一需要做的就是將您的列表框綁定到您的ViewModel的集合。

相關問題