如何显示数据绑定或覆盖ToString() c# WPF XAML

本文关键字:ToString WPF XAML 覆盖 数据绑定 何显示 显示 | 更新日期: 2023-09-27 18:03:24

我有这个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,它是作为Order的单一食品/饮料,但是可以包含Item和ObservableCollection,它是带有附加补充的食品/饮料。

简单的例子:我点了一份加糖加奶油的咖啡,我想这样展示给他们:咖啡(OrderItem)糖+奶油(补充)1、50 (OrderPrice)在这种情况下,布局不重要

我的问题是如何在视图中显示数据。我有两个想法,不知道如何继续。

  1. DataTemplate with textblock,每个textblock都绑定到Order类的一个属性,但是不知道如何显示ObservableCollection中的每一个item的名称。

    <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。重写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>

如何显示数据绑定或覆盖ToString() c# WPF XAML

至于选项#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与IValueConverter

[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}}"/>