为WPF设计数据模板

4
使用WPF、XAML、VS2008和Blend 2(或3 Beta更佳),您创建数据模板的过程是什么?您是否有测试数据模板外观的流程,而无需启动应用程序来测试数据外观?在Blend中有没有可以使用的流程,使开发数据模板更具图形化?
2个回答

6
您可以通过Blend在设计时指定数据,或者(为了使其在VS中也能正常工作)执行以下操作:
  • 创建一个对象的子类,将其设置为您的DataContext。
  • 在此子类的构造函数中,将属性设置为一些测试值。
  • 声明子类的一个实例作为资源。
  • 将DataContext设置为此资源。
  • 请记得在运行时清除或设置DataContext为合理的内容,否则用户将看到您的设计时数据。

在Silverlight中也适用。

以下是示例代码:
// The object (in a list) that'll be bound as our ListBox ItemsSource
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

// Our design-time data. Note that we add list items in the constructor
public class PersonDesignTimeData : ObservableCollection<Person>
{
    public PersonDesignTimeData()
    {
        this.Add(new Person { FirstName = "Fred", LastName = "Smith" });
        this.Add(new Person { FirstName = "Jim", LastName = "Brown" });
        this.Add(new Person { FirstName = "Dave", LastName = "Jones" });
    }
}

Window1.xaml:

<Window x:Class="DesignTimeDataDemo.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DesignTimeDataDemo"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:PersonDesignTimeData x:Key="PersonDesignTimeData"/>
    </Window.Resources>
    <Grid x:Name="root" DataContext="{StaticResource PersonDesignTimeData}">
        <ListBox
            ItemsSource="{Binding}"
            >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Width="200">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="2*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding FirstName}"/>
                        <TextBlock Grid.Column="1" Text="{Binding LastName}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

2
我不建议在设计时使用上述解决方案。请使用混合设计时库,它们可以在Visual Studio中工作,并且在SDK中可以轻松获得。 上述方法将在运行时消耗资源实例的内存,这只会在设计时实例化类。 以上例为基础,您需要像之前的答案一样操作,但在XAML中引用它,如下所示:
<Window x:Class="DesignTimeDataDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DesignTimeDataDemo"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
mc:Ignorable="d"
xmlns:DesignInstances="clr-namespace:Mrwa.Mmis.Field.Client.Feature.Defect.ViewModel"
d:DataContext="{d:DesignInstance IsDesignTimeCreatable=True, Type=DesignInstances:PersonDesignTimeCreatable}"

Title="Window1" Height="300" Width="300">
<Grid x:Name="root" >
    <ListBox
        ItemsSource="{Binding}"
        >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Width="200">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="2*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding FirstName}"/>
                    <TextBlock Grid.Column="1" Text="{Binding LastName}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

</Grid>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接