Yulian Linev Yulian Linev - 3 months ago 26
C# Question

How to display data-Binding or Override ToString() C# WPF XAML

I have this Order class and I am using MVVM:

namespace drinksMVVM.Models
{
public class Order : INotifyPropertyChanged
{
private Item _OrderItem;
private ObservableCollection<Item> _Supplements;
private double _OrderPrice;

public Item OrderItem
{
get
{
return _OrderItem;
}
set
{
_OrderItem = value;
OnPropertyChanged("OrderItem");
_OrderPrice += _OrderItem.Price;
}
}

public ObservableCollection<Item> Supplements
{
get
{
return _Supplements;
}
set
{
_Supplements = value;
OnPropertyChanged("Supplements");
foreach(var supplement in _Supplements)
{
_OrderPrice += supplement.Price;
}
}
}

public double OrderPrice
{
get
{
return _OrderPrice;
}
set
{
_OrderPrice = value;
OnPropertyChanged("Price");
}
}

public Order()
{

}

public Order(Item item, ObservableCollection<Item> items)
{
_OrderItem = item;
_Supplements = items;
}

public event PropertyChangedEventHandler PropertyChanged;

public void OnPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}


It contains object from type Item(properties Name and Price) and ObservableCollection of type Item. The Order object certainly contains one Item which is a single food/drink as order, but can contain Item and ObservableCollection, which is a food/drink with additional supplements.

Simple example:
I have order of Coffee with Sugar and Cream and I want to display them something like that:
Coffee (OrderItem)
Sugar+Cream (Supplements)
1,50 (OrderPrice)
The layout does not matter in this case

My question here is how to display the data in my view. I have two ideas which don't know how to continue.


  1. DataTemplate with textblocks, each textblock is bind to a property of the Order class, but don't know how to display every single item name from the ObservableCollection.

    <DataTemplate x:Key="OrderTemplate">
    <StackPanel Orientation="Vertical">
    <TextBlock x:Name="OrderName" Text="{Binding OrderItem.Name}"/>
    <TextBlock x:Name="OrderSupplements"
    Text="{Binding Supplements[0].Name}"/> ///Can't solve this
    binding
    <TextBlock x:Name="OrderPrce" Text="{Binding OrderPrice}"/>
    </StackPanel>
    </DataTemplate>



2.Override the ToString() method of the Order class in order to display all of the data in one single textblock, but also can't find a solution how to display all supplements names.

public override string ToString()
{
return OrderItem.Name + " " +/*every single supplement name*/+" "
+OrderPrice.ToString();
}


And

<DataTemplate x:Key="OrderTemplate1">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="test" Text="{Binding}"/>
</StackPanel>
</DataTemplate>

Answer

As far as option #2 goes...

public override string ToString()
{
    string orderStr = string.Empty;
    orderStr += _OrderItem.Name + " ";

    foreach(Item supplement in Supplements)
    {
        orderStr += supplement.Name + " ";
    }

    orderStr += OrderPrice.ToString();
    return orderStr;
}