如何在设计时将 Sketchflow 示例数据用于 ListBoxItem 模板?
我正在使用 Expression Blend 4 和 Visual Studio 2010 创建 Sketchflow 原型。
我有一个示例数据集合和一个与其绑定的列表框。这在设计时和运行时都按我的预期显示。然而,ListBoxItem 模板足够复杂,我想将其提取到自己的 XAML 文件中。即使项目仍然按预期在使用模板的主列表框中呈现,但当我打开模板本身时,所有数据绑定控件都是空的。
如果我向模板添加 DataContext,则可以在模板中查看并使用填充的对象,但本地 DataContext 会覆盖列表框中设置的 DataContext。
一些代码将说明这一点。首先创建一个 Sketchflow 项目(我使用的是 Silverlight,但它对于 WPF 应该同样工作),然后添加一个名为 SampleDataSource 的项目数据源。添加一个名为 ListData 的集合,其中包含一个名为 Title 的字符串属性。
这是主 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,该模板在其自己的 DemoListBoxItemTemplate.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.DemoListBoxItemTemplate">
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding Title}" Style="{StaticResource BasicTextBlock-Sketch}" Width="150"/>
</Grid>
</UserControl>
显然,这比我的要简单得多实际的列表框,但它应该足以说明我的问题。当您在表达式设计器中打开 Main.xaml 时,列表框将填充示例数据。但是,当您打开 DemoListBoxItemTemplate.xaml 时,没有数据上下文,因此没有可显示的数据,这使得直观地识别控件变得更加困难。
在使用模板时如何显示示例数据,同时仍允许将更大的示例数据集用于列表框本身?
I am using Expression Blend 4 and Visual Studio 2010 to create a Sketchflow prototype.
I have a Sample Data collection and a ListBox that is bound to it. This displays as I would expect both at design time and at run time. However, the ListBoxItem template it just complex enough that I wanted to pull it out into its own XAML file. Even though the items still render as expected in the main ListBox where the template is used, when I open the template itself, all of the databound controls are empty.
If I add a DataContext to the template, I can see and work with the populated objects while in the template, but then that local DataContext overrides the DataContext set on the listbox.
A bit of code will illustrate. Start by creating a Sketchflow project (I am using Silverlight, but it should work the same for WPF), then add a project data source called SampleDataSource. Add a collection called ListData, with a single String property called Title.
Here is the (scaled down) code for the main Sketchflow screen, which we'll call 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>
You can see that it references the DemoListBoxItemTemplate, which is defined in its own DemoListBoxItemTemplate.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.DemoListBoxItemTemplate">
<Grid x:Name="LayoutRoot">
<TextBlock Text="{Binding Title}" Style="{StaticResource BasicTextBlock-Sketch}" Width="150"/>
</Grid>
</UserControl>
Obviously, this is way simpler than my actual listbox, but it should be enough to illustrate my problem. When you open Main.xaml in the Expression designer, the list box is populated with sample data. But when you open DemoListBoxItemTemplate.xaml, there is no data context and therefore no data to display—which makes it more difficult to identify controls visually.
How can I have sample data displayed when I am working with the template, while still allowing the larger set of sample data to be used for the ListBox itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信这应该对你有用,我只是用 SL 和 Blend 4 尝试过:
将其放入的用户控件
单独的文件,移动模板
到它自己的资源字典中。
现在,您的模板位于单独的文件中,并且仍然可以使用数据上下文进行编辑,尽管它不会像数据模板位于同一用户控件中那样位于画板上。
希望有帮助,如果没有,请告诉我。
I believe this should work for you, I just tried it with SL and Blend 4:
a usercontrol to put it into a
separate file, move the template
into its own resource dictionary.
Now your template is in a separate file, and should still be editable with a datacontext, although it will not be in place on the artboard like it is if the data template is in the same usercontrol.
Hope that helps, let me know if it doesn't.