2013-03-05 125 views
1

我是WPF的新手,所以请接受我的道歉,如果我的问题是愚蠢的。如何将自定义对象绑定到WPF中的ListBox

我正在创建一个由2个主要部分组成的食品订购系统,即“食品菜单”和“订单列表”。当用户从食物菜单中选择一个项目时,它将被添加到代表订单列表的列表框控件中。

我已经创建了一些自定义对象:OrderOrderLineItem,以及集合类,OrderLineCollection,对于“订单行”。他们看起来像下面这样:

public class Order 
{ 
    public string ID { get; set; } 
    public DateTime DateTime { get; set; } 
    public double TotalAmt { get; set; } 
    public OrderLineCollection LineItems { get; set; } 
} 

public class OrderLine 
{ 
    public Item Item { get; set; } 
    public double UnitPrice { get; set; } 
    public int Quantity { get; set; } 
    public double SubTotal { get { return unitPrice * quantity; } } 
} 

[Serializable] 
public class OrderLineCollection : CollectionBase 
{ 
    public OrderLine this[int index] 
    { 
     get { return (OrderLine)List[index]; } 
     set { List[index] = value; } 
    } 
} 

public class Item 
{ 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public double UnitPrice { get; set; } 
    public byte[] Image { get; set; } 
} 

ListBox控制具有DataTemplate让更多详细信息将显示为每个项目。在XAML如下:

<Page x:Class="FoodOrdering.OrderList" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Order List"> 
    <ListBox Name="lbxOrder" HorizontalContentAlignment="Stretch">    
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid Margin="5"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="*"/> 
         <ColumnDefinition Width="30"/> 
         <ColumnDefinition Width="80"/> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
         <RowDefinition Height="40"/> 
        </Grid.RowDefinitions> 
        <TextBlock Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Text="{Binding item.name}"/> 
        <TextBlock Grid.Row="0" Grid.Column="1" Text="x "/> 
        <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding quantity}" Margin="10,0,0,0"/> 
        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding subTotal, Converter={StaticResource priceConverter}}" HorizontalAlignment="Right"/> 
        <Button Grid.Row="1" Grid.Column="2" Margin="0,5,0,0" Click="btnDelete_Click">Delete</Button> 
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Page> 

所以加入项目时,列表框将类似于下图: https://dl.dropbox.com/u/6322828/orderList.png

在代码隐藏,我创建了一个静态变量currentOrder用于存储当前顺序,它将被其他类/方法使用。

每次更改其值时,将调用以下愚蠢的方法LoadCurrentOrder()来刷新ListBox视图。

public partial class OrderList : Page 
{ 
    public static Order currentOrder = new Order(); 
    public OrderList() 
    { 
     InitializeComponent(); 
     LoadCurrentOrder(); 
    } 

    public void LoadCurrentOrder() 
    { 
     lbxOrder.Items.Clear(); 
     foreach (OrderLine ol in currentOrder.LineItems) 
      lbxOrder.Items.Add(ol); 
    } 
} 

我的问题是如何可以结合在一种优雅的方式的数据(例如使用ResourcesItemsSource等等),而不是使用上述方法,使得列表框将自动每次更新的值变量是否改变?

我试图

lbxOrder.ItemsSource = currentOrder; 

,但它不工作作为currentOrder不是System.Collections.IEnumerable对象。

回答

4

1 - 不创建自己的集合类型,.NET框架基类库已经有你需要的几乎一切。

删除OrderLineCollection并使用ObservableCollection<OrderLine>

2 - 尝试坚持使用MVVM约定。这意味着你不应该在代码中操作UI元素。

public OrderList() 
{ 
    InitializeComponent(); 
    LoadCurrentOrder(); 
    DataContext = this; 
} 

然后在XAML:

<ListBox ItemsSource="{Binding LineItems}" HorizontalContentAlignment="Stretch"> 

3 - 除非你需要参考他们在XAML不要将其命名在XAML UI元素。这将帮助你每一次你发现自己想操纵程序代码的UI元素的时间重新考虑你的方法。

4 - 习惯了C#命名约定。属性名称应该是“合适的装箱”(I.E LineItems而不是lineItems

+0

感谢大会的提示特别是命名一个,想我搞砸了很长一段时间的概念......我已编辑的情况下,其他的属性名称可能会感到困惑 'ObservableCollection'在我的WPF项目工作正常。但是如果我在类库项目中创建类,该怎么办?我试过了,它看起来像'ObservableCollection'只支持XAML用法? – pblyt 2013-03-06 10:58:39

+0

@pblyt的['System.Collections.ObjectModel.ObservableCollection '](http://msdn.microsoft.com/en-us/library/ms668604(V = VS.100)的.aspx)类中定义的' System.dll'组件,因此其是.NET BCL的核心部分,它不以任何方式依赖于任何WPF组件或类。 – 2013-03-06 14:54:08

0

您只需将lineItems属性设置为ObservableCollection<yourType>即可。然后,当此集合发生更改(项目添加,删除)时,列表框将自动刷新。

如果您的问题是订单本身发生变化,您需要刷新列表框。然后,您只需执行INotifyPropertyChanged,并在订单更改时触发属性已更改的通知,并且UI将通过绑定进行刷新。

至于更优雅的方式绑定。您可以绑定到currentOrder.lineItems

lbxOrder.ItemsSource = currentOrder.lineItems;//this should be an observable collection if you intend to have items change 
+0

谢谢@dutzu,'ObservableCollection'在我的WPF项目中工作正常。但是,如果我在类库项目中创建类并将其添加为WPF项目中的参考,该怎么办?我试过了,它看起来像'ObservableCollection'只支持XAML用法。 – pblyt 2013-03-06 11:06:49

+0

您可以随时换你从类库得到作为一个ObservableCollection以满足您的需求,用户界面对象的名单。 – dutzu 2013-03-06 12:08:45

相关问题