Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 如何在设计时为ListBoxItem模板使用Sketchflow示例数据?_Silverlight_Expression Blend_Sketchflow_Sample Data - Fatal编程技术网

Silverlight 如何在设计时为ListBoxItem模板使用Sketchflow示例数据?

Silverlight 如何在设计时为ListBoxItem模板使用Sketchflow示例数据?,silverlight,expression-blend,sketchflow,sample-data,Silverlight,Expression Blend,Sketchflow,Sample Data,我正在使用Expression Blend 4和Visual Studio 2010创建Sketchflow原型 我有一个样本数据收集和一个绑定到它的列表框。这在设计时和运行时都会显示出来。然而,ListBoxItem模板非常复杂,我想把它拉到自己的XAML文件中。即使这些项在使用模板的主列表框中仍按预期呈现,但当我打开模板本身时,所有数据绑定控件都是空的 如果将DataContext添加到模板中,我可以在模板中查看并使用填充的对象,但是本地DataContext会覆盖列表框上的DataCont

我正在使用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时,没有数据上下文,因此没有数据可显示,这使得从视觉上识别控件更加困难


如何在使用模板时显示样本数据,同时仍允许将更大的样本数据集用于列表框本身?

我相信这对您应该有用,我刚刚用SL和Blend 4进行了尝试:

  • 而不是将模板制作成 将其放入 分离文件,移动模板 进入它自己的资源字典
  • 要添加新的资源字典,在“资源”面板的右上角有一个按钮
  • 在usercontrol的资源中找到模板(默认情况下可能命名为ItemTemplate),右键单击它进行复制,将其粘贴到新的资源字典中
  • 删除原始资源,这可能会给您一个关于引用的警告,如果您保持它们不变,它们可能仍然有效,因为名称将相同,如果不相同:
  • 右键单击列表框,编辑其他模板、项目模板、应用资源,然后从新的资源字典中选择项目模板
  • 现在,您的模板位于一个单独的文件中,并且应该仍然可以使用datacontext进行编辑,尽管它不会像数据模板位于同一个usercontrol中那样位于artboard上


    希望这有帮助,如果没有,请告诉我。

    谢谢,我尝试了这个建议,但最终得到了非常相似的结果:我在资源上设置的任何datacontext(以便在设计时可以看到内容的布局)都会覆盖列表框的datacontext集(我希望在运行时看到)。不过,我真的很感谢你的详尽答复!资源不需要单独的datacontext,它应该继承正在编辑的datacontext。