2011-08-01 147 views
0

我有一個Silverlight數據網格,允許用戶輸入您計劃訂購的產品數量。已經填充的是他的物品成本。現在我有一個事件,一旦完成編輯後,單元格會檢查該標題,但是如何將這兩個單元格相乘並將該值放入成本列中的某個單元格中。我知道我真的會改變綁定到我的數據網格的對象列表,但我不知道如何做到這一點。乘法Silverlight Datagrid

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" /> 
+0

您可以更改綁定到您的數據網格的對象,以便所需的屬性自動計算爲成本*金額,然後它應該自動更新。 – Tsabo

回答

1

您可以修改綁定到DataGrid中的對象,包括總成本的特性。例如,假設你的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(),    
     } 
    }   

); 
+0

所以我添加了代碼,它什麼都不做。難道是因爲我通過LINQ填充datagrid或者我沒有set屬性? –

+0

你可以發佈定義你的數據網格的XAML嗎?如果你在代碼中設置DataGrid,你是否也可以發佈該代碼? – fsong

+0

我添加了我的代碼,我將在上午添加我的Xaml。 –