2009-06-15 13 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ですぐに使用できます。 上記の方法では、リソースインスタンスの実行時にメモリが消費されます。これは、クラスをインスタンス化してインスタンス化するだけです。

<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> 

:あなたは前の回答と同じことを行うが、このようなXAMLでそれを参照しますベースとして上記の例を使用

関連する問題