乘法Silverlight数据网格

本文关键字:网格 数据网 数据 Silverlight 乘法 | 更新日期: 2023-09-27 18:04:10

我有一个Silverlight数据网格,允许用户输入计划订购的产品数量。已经填充的是项目的成本。现在我有一个事件,一旦你完成编辑单元格,它会检查标题,但是我如何将这两个单元格相乘,并将值放在成本列的单元格中。我知道我真的会改变绑定到我的数据网格的对象列表,但我不知道如何做到这一点。

 client.GetOrderCompleted += (s, ea) =>
        {
            dgOrder.AutoGenerateColumns = false;
            //dgOrder.ColumnWidth.Value = 100;


            dgOrder.Columns.Add(CreateTextColumn("SKU", "SKU"));
            dgOrder.Columns.Add(CreateTextColumn("productname", "Product Name"));
            dgOrder.Columns.Add(CreateTextColumn("itemnumber", "Item Number"));
            dgOrder.Columns.Add(CreateTextColumn("cost", "Cost"));
            dgOrder.Columns.Add(CreateTextColumn("asin", "ASIN"));
            dgOrder.Columns.Add(CreateTextColumn("pendingorder", "Rank"));
            dgOrder.Columns.Add(CreateTextColumn("rank", "Node"));
            //dgOrder.Columns.Add(CreateTextColumn("w4", "AMZN"));
            dgOrder.Columns.Add(CreateTextColumn("amazon", "AMZN"));
            dgOrder.Columns.Add(CreateTextColumn("ourprice", "OurPrice"));
            dgOrder.Columns.Add(CreateTextColumn("bbprice", "BuyBox"));
            dgOrder.Columns.Add(CreateTextColumn("afner", "AFN"));
            dgOrder.Columns.Add(CreateTextColumn("quantity", "INV"));
            dgOrder.Columns.Add(CreateTextColumn("w4", "W4"));
            dgOrder.Columns.Add(CreateTextColumn("w3", "W3"));
            dgOrder.Columns.Add(CreateTextColumn("w2", "W2"));
            dgOrder.Columns.Add(CreateTextColumn("w1", "W1"));
            dgOrder.Columns.Add(CreateTextColumn("order", "Order"));
            dgOrder.Columns.Add(CreateTextColumn("total", "Total"));
            dgOrder.Columns.Add(CreateTextColumn("profit", "Profit"));
            dgOrder.Columns.Add(CreateTextColumn("percent", "Percent"));
            dgOrder.Columns.Add(CreateHyperlink("asin"));
            dgOrder.ItemsSource = ea.Result;
            Original = ea.Result;
        };
    client.GetOrderAsync(txtBox.Text);
XAML:

<sdk:DataGrid AutoGenerateColumns="True" 
      Height="469" 
      HorizontalAlignment="Left" 
      Margin="12,41,0,0" 
      Name="dgOrder" 
      VerticalAlignment="Top" 
      Width="1316" 
      HeadersVisibility="All" 
      CellEditEnded="dgOrder_CellEditEnded" 
      RowBackground="DodgerBlue" 
      AlternatingRowBackground="LightBlue"
      LostFocus="TextBox_LostFocus" 
      BeginningEdit="dgOrder_BeginningEdit" 
      SelectionChanged="dgOrder_SelectionChanged"
      LoadingRow="dgOrder_LoadingRow" />

乘法Silverlight数据网格

您可以修改绑定到数据网格的对象,并包含总成本的属性。例如,假设您的DataGrid绑定到一个IList,您可以添加一个新的TotalCost属性:

public class ItemOrder
{
    public double TotalCost
    {
        get
        {
            return this.OrderAmount * this.ItemCost;
        }
    }
    // rest of your class
}

或者,您可以使用值转换器来显示总成本:

<DataGrid>
    <DataGrid.Resources>
        <TotalCostConverter x:Key="TotalCostConverter"/>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Amount" Binding="{Binding OrderAmount}"/>
        <DataGridTextColumn Header="ItemCost" Binding="{Binding ItemCost}"/>
        <DataGridTextColumn Header="Total" Binding="{Binding Converter={StaticResource TotalCostConverter}"/>
    </DataGrid.Columns>
</DataGrid>

//Converter
public class TotalCostConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var itemOrder = value as ItemOrder;
        return itemOrder.OrderAmount * itemOrder.ItemCost;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
编辑:在查看代码之后,您可以向绑定到ItemOrder对象的数据网格中添加一个新列,并将值转换器附加到它上面。
dgOrder.Columns.Add(
    new DataGridTextColumn()
    { 
        Header = "Total",
        Binding = new Binding() 
        {
            //The Value converter described above
            Converter = new TotalCostConverter(),             
        }
    }        
);