我有这个Order类,我正在使用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));
}
}
}
}
它包含Item类型的属性(属性Name和Price)和Item类型的ObservableCollection。 Order对象当然包含一个Item,它是一个单独的食物/饮料作为订单,但可以包含Item和ObservableCollection,这是一个带有额外补充剂的食物/饮料。
简单示例: 我有咖啡加糖和奶油的订单,我想展示他们的东西: 咖啡(OrderItem) 糖+奶油(补品) 1,50(OrderPrice) 在这种情况下,布局无关紧要
我的问题是如何在我的视图中显示数据。我有两个想法,不知道如何继续。
带有文本块的DataTemplate,每个文本块都绑定到Order类的属性,但不知道如何显示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 Order类的ToString()方法,以便在一个文本块中显示所有数据,但也无法找到如何显示所有补充名称的解决方案。
public override string ToString()
{
return OrderItem.Name + " " +/*every single supplement name*/+" "
+OrderPrice.ToString();
}
和
<DataTemplate x:Key="OrderTemplate1">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="test" Text="{Binding}"/>
</StackPanel>
</DataTemplate>
答案 0 :(得分:0)
就选项#2而言......
public override string ToString()
{
string orderStr = string.Empty;
orderStr += _OrderItem.Name + " ";
foreach(Item supplement in Supplements)
{
orderStr += supplement.Name + " ";
}
orderStr += OrderPrice.ToString();
return orderStr;
}
答案 1 :(得分:0)
我设法用IValueConverter
解决了选项#1[ValueConversion(typeof(List<Item>),typeof(string))]
public class MyConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
List<Item> items = (List<Item>)value;
string supplementsString = string.Empty;
if (items.Count >= 1)
{
foreach (var item in items)
{
supplementsString += item.Name + " " + item.Price.ToString() + " " + Environment.NewLine;
}
}
return supplementsString;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
和XAML部分
<converters:MyConverter x:Key="MyConverterTest"/>
<TextBlock x:Name="OrderSupplements"
Text="{Binding Supplements, Converter={StaticResource MyConverter}}"/>