Silverlight 4.0 MainPage.xaml中的Silverlight用户控件

Silverlight 4.0 MainPage.xaml中的Silverlight用户控件,silverlight-4.0,datacontext,Silverlight 4.0,Datacontext,我目前正在研究在ExpressionBlend 4中的Silverlight项目中构建用户控件。控件具有一组与用户控件相关联的示例数据,并且这些数据在用户控件中正确显示 在主页面上放置用户控件时,示例数据不会显示在用户控件中。这是正确的行为,还是我设置了/没有设置什么?我发现奇怪的是,当我编辑用户控件时,数据出现在主页面上的重建指示器旁边(黄色感叹号)。重建时,数据再次消失 这是主页代码: <UserControl xmlns="http://schemas.microsoft.com/w

我目前正在研究在ExpressionBlend 4中的Silverlight项目中构建用户控件。控件具有一组与用户控件相关联的示例数据,并且这些数据在用户控件中正确显示

在主页面上放置用户控件时,示例数据不会显示在用户控件中。这是正确的行为,还是我设置了/没有设置什么?我发现奇怪的是,当我编辑用户控件时,数据出现在主页面上的重建指示器旁边(黄色感叹号)。重建时,数据再次消失

这是主页代码:

<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:SilverlightApplication2" mc:Ignorable="d"
x:Class="SilverlightApplication2.MainPage"
Width="1200" Height="640">
<UserControl.Resources>
    <local:MultiDayViewModel x:Key="MultiDayViewModelDataSource" d:IsDataSource="True"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White" d:DataContext="{d:DesignData /SampleData/TestSampleData.xaml}">
    <Grid.RowDefinitions>
        <RowDefinition Height="0.128*"/>
        <RowDefinition Height="0.872*"/>
    </Grid.RowDefinitions>
    <StackPanel Margin="0,24,8,8" HorizontalAlignment="Right" Width="318" Orientation="Horizontal">
        <Button Content="Daily"/>
        <Button Content="Weekly"/>
    </StackPanel>
    <local:MultiDayView x:Name="MultiDayView" Margin="8" Grid.Row="1" DataContext="{Binding Calenar, Source={StaticResource MultiDayViewModelDataSource}}"/>
</Grid>

如有任何想法或指示,将不胜感激


谢谢。

您使用的是d:DataContext,它仅在设计模式下显示。当您将控件放置在MainPage中时,Blend会将其解释为处于运行时模式,因此数据不会显示,因此这是预期的行为

在Blend中为控件创建样本数据时,可以指定是否希望在运行时也使用此样本数据,或者只需设置DataContext属性而不是d:DataContext属性,或者除了d:DataContext属性之外

下图显示了从Blend创建样本数据源时,如何在运行时启用样本数据:

当您选择名为“在应用程序运行时启用示例数据”的选项时,您的XAML如下所示:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="ASD_Answer002.MainPage"
        Width="640" Height="480">
        <UserControl.Resources>
            <DataTemplate x:Key="DataTemplate1">
                <CheckBox Content="{Binding Property1}" IsChecked="{Binding Property2, Mode=TwoWay}"/>
            </DataTemplate>
        </UserControl.Resources>

        <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
            <ItemsControl ItemsSource="{Binding Collection}" ItemTemplate="{StaticResource DataTemplate1}" Margin="50"/>
        </Grid>
    </UserControl>


这将显示设计时和运行时的示例数据。

运行这两种数据时都不会显示示例数据。我本来想单独问这个问题,但我觉得这两个问题是相关的。有什么线索吗?谢谢你,Murven,我尝试过这种方法,但是编译器不喜欢DataContext引用,因为“DesignData”不在Blend 2008命名空间中。这是正确的,我已经更新了我的答案,以包括当您在Blend中使用在运行时启用示例数据的选项时XAML的外观。我还注意到您正在声明local:MultiDayViewModel的一个实例。您可以使该实例为您的属性返回模拟数据,如果您将DataContext和d:DataContext都设置为指向该视图模型实例,那么这将在设计和运行时显示数据。如果创建的ViewModelLocator在设计时返回模拟视图模型,在运行时返回真实视图模型,则此选项更有用。