2009-06-15 56 views
4

使用WPF,XAML,VS2008和Blend 2(或首選3 Beta版),您創建數據模板的過程是什麼?您是否有測試數據模板外觀的過程,而不是僅僅爲了測試數據的外觀而旋轉您的應用程序?我可以在Blend中使用哪個過程來使開發數據模板更加圖形化?爲WPF設計數據模板

回答

6

您可以通過混合指定在設計時的數據,或(得到它在VS的工作,以及)做到這一點:

  • 創建您設置爲您的DataContext對象的子類。
  • 在此子類的構造函數中,將屬性設置爲某些測試值。
  • 聲明該子類的一個實例作爲資源。
  • 將DataContext設置爲此資源。
  • 請記住在運行時清除DataContext或將其設置爲合理的,否則用戶將看到您的設計時數據。

也適用於Silverlight。

下面是一些示例代碼:

// The object (in a list) that'll be bound as our ListBox ItemsSource 
public class Person 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

// Our design-time data. Note that we add list items in the constructor 
public class PersonDesignTimeData : ObservableCollection<Person> 
{ 
    public PersonDesignTimeData() 
    { 
     this.Add(new Person { FirstName = "Fred", LastName = "Smith" }); 
     this.Add(new Person { FirstName = "Jim", LastName = "Brown" }); 
     this.Add(new Person { FirstName = "Dave", LastName = "Jones" }); 
    } 
} 

Window1.xaml:

<Window x:Class="DesignTimeDataDemo.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:DesignTimeDataDemo" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <local:PersonDesignTimeData x:Key="PersonDesignTimeData"/> 
    </Window.Resources> 
    <Grid x:Name="root" DataContext="{StaticResource PersonDesignTimeData}"> 
     <ListBox 
      ItemsSource="{Binding}" 
      > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Width="200"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="2*"/> 
         </Grid.ColumnDefinitions> 
         <TextBlock Grid.Column="0" Text="{Binding FirstName}"/> 
         <TextBlock Grid.Column="1" Text="{Binding LastName}"/> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

    </Grid> 
</Window> 
2

我不會用設計時數據的上述解決方案。使用混合設計時庫,它們在Visual Studio中工作,並且可以在SDK中使用。 上述方式將在運行時爲資源實例消耗內存,這隻會在設計時實例化類。

使用上面的例子作爲基礎,你會做一樣以前的答案,但在XAML中這樣引用它:

<Window x:Class="DesignTimeDataDemo.Window1" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:DesignTimeDataDemo" 

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d" 
xmlns:DesignInstances="clr-namespace:Mrwa.Mmis.Field.Client.Feature.Defect.ViewModel" 
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=DesignInstances:PersonDesignTimeCreatable}" 

Title="Window1" Height="300" Width="300"> 
<Grid x:Name="root" > 
    <ListBox 
     ItemsSource="{Binding}" 
     > 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid Width="200"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="2*"/> 
        </Grid.ColumnDefinitions> 
        <TextBlock Grid.Column="0" Text="{Binding FirstName}"/> 
        <TextBlock Grid.Column="1" Text="{Binding LastName}"/> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</Grid>