简体   繁体   English

数据模板的绑定模型和视图模型

[英]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 ? 如何实现对ListViewDataTemplate使用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>
  1. You need to set the DataContext of your XAML to the viewmodel. 您需要将XAML 的DataContext设置为viewmodel。
  2. You need to raise the property changed event that is ultimately exposed via your ViewModelBase 您需要引发最终通过ViewModelBase公开的属性更改事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM