[英]Binding Model and ViewModel for a DataTemplate
I am relatively new to WPF and I try to show some data in a ListView
. 我对WPF比较陌生,我尝试在
ListView
显示一些数据。
I have a Model class Order
which I want to simply show on the ListView
. 我有一个Model类
Order
,我想简单地显示在ListView
。 But I want to also show some other calculated informations (such as amount of Positions
inside the order). 但我还想显示其他一些计算出的信息(例如订单内的
Positions
数量)。
How do I achieve to use the OrderViewModel
(ViewModel) and not the Order
(Model) class for the DataTemplate
of the ListView
? 如何实现对
ListView
的DataTemplate
使用OrderViewModel
(ViewModel)而不是Order
(Model)类?
Please tell me if you need more informations. 请告诉我您是否需要更多信息。
Thank you in advance! 先感谢您!
Order class 订单类别
This is my order class. 这是我的订单类。 It contains some information, but not everything I want to show on the ListView.
它包含一些信息,但不是我想在ListView上显示的所有信息。
public class Order
{
private int number;
private ObservableCollection<Position> positions;
/// <summary>
/// Default Constructor
/// </summary>
public Order()
{
this.positions = new ObservableCollection<Position>();
}
/// <summary>
/// Order Number
/// </summary>
public int Number
{
get { return number; }
set { number = value; }
}
/// <summary>
/// Positions inside the order.
/// </summary>
public ObservableCollection<Position> Positions
{
get { return positions; }
set { positions = value; }
}
}
Order ViewModel 订单ViewModel
This class is the ViewModel I want to show on the ListView. 此类是我要在ListView上显示的ViewModel。
public class OrderViewModel : ViewModelBase
{
readonly private Order order;
#region Constructor
public OrderViewModel(Order order)
{
this.order = order;
}
#endregion Constructor
#region Order Properties
public string Number
{
get { return order.Number.ToString(); }
}
public ObservableCollection<Position> Positions
{
get
{
return order.Positions;
}
}
#endregion Order Properties
#region Presentation Properties
public int AmountOfPositions // Calculated value should be shown on the ListView.
{
get { return order.Positions.Count; }
}
#endregion Presentation Properties
}
XAML of the ListView ListView的XAML
<ListView x:Name="orderListView" ItemsSource="{Binding Path=AvailableOrders}" Margin="0,54,0,0" SelectionMode="Single"
SelectedItem="{Binding Path=SelectedOrder}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Number}" />
<TextBlock Text="{Binding AmountOfPositions}" // This does not work! />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.