对于那些在实际项目中使用 Expression Blend和 Visual Studio 的人,请帮助我了解您如何在日常开发/设计任务中使用 Blend 和 Visual Studio,这是一个真实的场景:
我在 Visual Studio 中创建了以下简单的 WPF 应用程序。它显示了一个客户对象列表,其中包含一个以简单的橙色框显示客户的 DataTemplate 。
我现在想通过使用 Expression Blend在这个 DataTemplate 中添加一些 pizazz 。
我在 Expression Blend 中打开项目,认为我将看到可以更改颜色的橙色框,当我将鼠标悬停在它们上时创建动画,调整它的大小等。但是,我在 Expression Blend 中看到的只是一个完全空白的盒子。
所以我明白:
- Expression Blend似乎无法理解我的数据来自 ViewModel,因此不会显示它。这是 Blend 的一个限制,还是我需要以某种方式更改我的代码,以便 Blend 可以解释运行时将输出的数据?
- 我正在使用具有“示例数据”功能的 Expression Blend 3。使用此示例数据功能的最佳方法是什么,以便即使它无法解释 C# 并理解 ViewModel 属性将从 ViewModel 属性中输出的数据来填充列表框,我怎样才能让它至少产生一些虚拟数据以便我可以操作 DataTemplate?
XAML:
<Window x:Class="TestStringFormat234.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="DataTemplateCustomers">
<Border CornerRadius="5" Background="Orange" Padding="5" Margin="3">
<StackPanel Orientation="Horizontal">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1} (hired on {2:MMM dd, yyyy})">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
<Binding Path="HireDate"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding GetAllCustomers}"
ItemTemplate="{StaticResource DataTemplateCustomers}">
</ListBox>
</Grid>
</Window>
代码背后:
using System.Windows;
using System.Collections.ObjectModel;
using System;
namespace TestStringFormat234
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = new CustomerViewModel();
}
}
//view model
public class CustomerViewModel
{
public ObservableCollection<Customer> GetAllCustomers {
get {
ObservableCollection<Customer> customers = new ObservableCollection<Customer>();
customers.Add(new Customer { FirstName = "Jim", LastName = "Smith", HireDate = DateTime.Parse("2007-12-31") });
customers.Add(new Customer { FirstName = "Jack", LastName = "Jones", HireDate = DateTime.Parse("2005-12-31") });
return customers;
}
}
}
//model
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime HireDate { get; set; }
}
}