Expression Blend 4 と Visual Studio 2010 を使用して、Sketchflow プロトタイプを作成しています。
Sample Data コレクションとそれにバインドされた ListBox があります。これは、設計時と実行時の両方で期待どおりに表示されます。ただし、ListBoxItem テンプレートは非常に複雑なので、独自の XAML ファイルに抽出する必要がありました。テンプレートが使用されているメインの ListBox でアイテムが期待どおりにレンダリングされていても、テンプレート自体を開くと、データバインドされたすべてのコントロールが空になります。
テンプレートに DataContext を追加すると、テンプレート内で入力されたオブジェクトを表示して操作できますが、そのローカル DataContext はリストボックスに設定された DataContext をオーバーライドします。
少しのコードで説明します。まず、Sketchflow プロジェクトを作成し (私は Silverlight を使用していますが、WPF でも同じように機能するはずです)、SampleDataSource という名前のプロジェクト データ ソースを追加します。Title という単一の String プロパティを持つ ListData というコレクションを追加します。
以下は、Sketchflow のメイン画面の (縮小された) コードで、これを Main.xaml と呼びます。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DemoScreens"
mc:Ignorable="d"
x:Class="DemoScreens.Main"
Width="800" Height="600">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ProjectDataSources.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="ListBoxItemTemplate">
<local:DemoListBoxItemTemplate d:IsPrototypingComposition="True" Margin="0,0,5,0" Width="748"/>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#5c87b2" DataContext="{Binding Source={StaticResource SampleDataSource}}">
<ListBox Background="White" x:Name="DemoList" Style="{StaticResource ListBox-Sketch}" Margin="20,100,20,20" ItemTemplate="{StaticResource ListBoxItemTemplate}" ItemsSource="{Binding ListData}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>
</Grid>
</UserControl>
独自の DemoListBoxItemTemplate.xaml で定義されている DemoListBoxItemTemplate を参照していることがわかります。
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DemoScreens"
mc:Ignorable="d"
x:Class="DemoScreens.DemoListBoxItemTemplate">
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding Title}" Style="{StaticResource BasicTextBlock-Sketch}" Width="150"/>
</Grid>
</UserControl>
明らかに、これは実際のリストボックスよりもはるかに単純ですが、問題を説明するには十分なはずです。式デザイナーで Main.xaml を開くと、リスト ボックスにサンプル データが入力されます。しかし、DemoListBoxItemTemplate.xaml を開くと、データ コンテキストがないため、表示するデータがないため、コントロールを視覚的に識別するのが難しくなります。
テンプレートを使用しているときにサンプル データを表示しながら、より多くのサンプル データ セットを ListBox 自体に使用できるようにするにはどうすればよいですか?