MainPage.xaml 中的 Silverlight 用户控件

发布于 2024-09-28 09:21:10 字数 1525 浏览 7 评论 0原文

我目前正在研究在 Expression Blend 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>

任何想法或指示将不胜感激。

谢谢。

I'm currently investigating building a User Control in a Silverlight Project in Expression Blend 4. The control has an associated set of sample data for the User Control, and the data is appearing correctly in the User Control.

When I place the User Control on the main page, the sample data does not appear in the User Control. Is this correct behaviour, or am I setting/not setting something? What I am finding odd is that when I edit the User Control, the data appears in the Main Page alongside a Rebuild Indicator (Yellow Exclamation mark). When I rebuild, the data disappears again.

This is the Main Page Code:

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

Any thoughts or directions would be appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-10-05 09:21:10

您正在使用 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>

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

You are using d:DataContext, which only appears in design mode. When you place the control inside MainPage, it is interpreted by Blend as being in runtime mode, so the data does not appear, so this is expected behavior.

When you create the sample data for you control in Blend you can specify whether you want this sample data to be used during runtime as well, or you can simply set the DataContext property instead of or in addition to the d:DataContext property.

The following image shows how you can enable sample data during runtime, when you create the sample data source from Blend:

Enable sample data when application is running

When you select the option called "Enable sample data when application is running", your XAML looks like this:

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

This will show your sample data for both design time and runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文