2015-05-05 29 views
0

我需要社區的幫助。我試圖乘以一個DataGrid(WPF和C#)中的兩列的值,第一列從MySql數據庫獲取其數據,第二列是輸入值,其中用戶將輸入一個數字,該數字應與第一列,結果應顯示在第三列「總計」中。我搜遍了各地,嘗試了不同的方法,嘗試了幾乎相同的東西,但我無法獲得增值的價值,並且結果出現在第三列中。下面是我試過的代碼的最後一點,我不得不提到我仍然很新的C#和WPF有沒有這麼多的經驗:DataGrid複製兩列

<DataGrid AutoGenerateColumns="False" x:Name="tblData" Margin="30,197,7,0" Grid.Row="1" VerticalAlignment="Top" Height="510" Grid.ColumnSpan="4" 
       BorderThickness="2" BorderBrush="#FF445BBF" ItemsSource="{Binding Path=LoadDataBinding}" CanUserResizeRows="False" ClipToBounds="True" 
       CanUserSortColumns="False" HorizontalGridLinesBrush="#FFC7C7C7" VerticalGridLinesBrush="#FFC7C7C7" IsManipulationEnabled="True" EnableRowVirtualization="False" 
       IsTextSearchEnabled="True" xmlns:local="clr-namespace:PoS_Pimentel"> 
     <DataGrid.Resources> 
      <local:AmmountConverter x:Key="AmmountConverter" /> 
     </DataGrid.Resources> 
     <DataGrid.Columns> 
      <DataGridTextColumn Binding="{Binding Path=nomprod}" Header="Producto" Width="500" IsReadOnly="True"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Binding="{Binding Path=preciogram, Mode=TwoWay}" Header="Precio por Gramo" Width="190" IsReadOnly="True"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Binding="{Binding Path=gramos, Mode=TwoWay}" Header="Gramos" Width="190" IsReadOnly="False"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Binding="{Binding Path=total, Mode=TwoWay}" Header="Total" Width="*" IsReadOnly="True"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

,並在C#最後,我創建了兩個獨立的cs文件對於EntitiyClass和AmmountConverter類:

EntityClass代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.ComponentModel; 
using System.Collections.ObjectModel; 
using System.Windows.Forms; 

namespace PoS 
{ 
    #region 
    public class Entity_Class : INotifyPropertyChanged 
    { 
     private int _preciogram; 
     public int PrecioGram 
     { 
      get { return _preciogram; } 
      set { _preciogram = value; NotifyPropertyChanged("gramos"); } 
     } 

     private int _gramos; 
     public int Gramos 
     { 
      get { return _gramos; } 
      set { _gramos = value; NotifyPropertyChanged("gramos"); } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     private void NotifyPropertyChanged(string propertyName) 
     { 
      if(PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

而且AmmountConverter類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Data; 

namespace PoS_Pimentel 
{ 
    public class AmmountConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      double prcgrms = values[1] == null ? 0 : System.Convert.ToDouble(values[1]); 
      double grms = values[2] == null ? 0 : System.Convert.ToDouble(values[2]); 

      return prcgrms * grms; 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

我不是很擅長這個,但我想和任何指針將不勝感激。謝謝你們。

+0

我甚至試圖從這個職位的解決方案,但它沒有工作,要麼: [鏈接](http://stackoverflow.com/questions/28257978/wpf-datagrid-calculations) – hectormtnezg

+0

你在哪裏使用AmmountConverter?在總列中,您沒有使用轉換器。 –

+0

噢,是的,對不起,我忘了包含這段代碼,但它告訴我AmmountConverter沒有找到。 hectormtnezg

回答

2

你不需要使用轉換器來做這個計算。希望你可以在Model Class中處理它。你也需要綁定到屬性。 DataGrid綁定屬性名稱不正確c#區分大小寫。請參考下面的代碼。

<DataGrid x:Name="dgr" AutoGenerateColumns="False" ItemsSource="{Binding LoadDataBinding}">   
     <DataGrid.Columns>     
      <DataGridTextColumn Binding="{Binding Path=PrecioGram, Mode=TwoWay}" Header="Precio por Gramo" Width="190" IsReadOnly="True"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Binding="{Binding Path=Gramos, Mode=TwoWay}" Header="Gramos" Width="190" IsReadOnly="False"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 
      </DataGridTextColumn> 
      <DataGridTextColumn Header="Total" Width="100" Binding="{Binding Total}"> 
       <DataGridTextColumn.HeaderStyle> 
        <Style TargetType="DataGridColumnHeader"> 
         <Setter Property="HorizontalContentAlignment" Value="Center" /> 
         <Setter Property="FontSize" Value="14" /> 
        </Style> 
       </DataGridTextColumn.HeaderStyle> 

      </DataGridTextColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new MainViewModel(); 
    } 

} 

public class MainViewModel 
{ 
    private ObservableCollection<Entity_Class> myVar = new ObservableCollection<Entity_Class>(); 

    public ObservableCollection<Entity_Class> LoadDataBinding 
    { 
     get { return myVar; } 
     set { myVar = value; } 
    } 

    public MainViewModel() 
    { 
     for (int i = 1; i < 10; i++) 
     { 
      LoadDataBinding.Add(new Entity_Class() { PrecioGram=i}); 
     } 
    } 
} 

public class Entity_Class : INotifyPropertyChanged 
{ 
    private int _preciogram; 
    public int PrecioGram 
    { 
     get { return _preciogram; } 
     set { _preciogram = value; NotifyPropertyChanged("PrecioGram"); } 
    } 

    private int _gramos; 
    public int Gramos 
    { 
     get { return _gramos; } 
     set 
     { 
      _gramos = value; NotifyPropertyChanged("gramos"); 
      Total = _preciogram * _gramos; 
     } 
    } 

    private int _total; 
    public int Total 
    { 
     get { return _total; } 
     set { _total = value; NotifyPropertyChanged("Total"); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
+0

這也可以正常工作,雖然我個人不喜歡我的屬性setter中的任何非標準代碼。我希望使用PropertyChanged事件處理程序:) – Rachel

+0

我希望propertychangedter也引發propertychanged事件:) –

+0

我發現這個工程很好,但循環中產生的數字(從0到9),它實際上相乘但它似乎並不希望與我從數據庫中提取的數據「預先圖像」 – hectormtnezg

1

有兩種方法可以做到這一點,它看起來像你試圖混合使用這兩種方法。

一種方法是將所有3列綁定到數據對象上的屬性,並在用戶輸入的屬性上使用PropertyChange事件處理程序來重新計算總列。這通常是我的偏好。

<DataGridTextColumn Binding="{Binding Value1}" /> 
<DataGridTextColumn Binding="{Binding Value2}" /> 
<DataGridTextColumn Binding="{Binding Total}" /> 
private void MyDataItem_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    if (e.PropertyName == "Value2") 
     Total = Value1 * Value2; 
} 

採用這種方法,不需要轉換器。


另一種方法是將第3列不結合,並且使用轉換器來顯示它

<!-- Note: syntax may be incorrect here --> 
<DataGridTextColumn Binding="{Binding Value1}" /> 
<DataGridTextColumn Binding="{Binding Value2}" /> 
<DataGridTextColumn> 
    <DataGridTextColumn.Binding> 
     <Multibinding Converter="{StaticResource MyMultiplicationConverter}"> 
      <Binding Path="Value1" /> 
      <Binding Path="Value2" /> 
     </Multibinding> 
    <DataGridTextColumn.Binding> 
</DataGridTextColumn> 

在這種情況下,我們兩列上的數據模型綁定到值,並且第三列使用轉換器將兩個值轉換爲不同的第三個值。


應當注意無論哪個是接近你使用,默認爲一個TextBox結合模式是隻在LostFocus更新源,所以我們沒有提出過多的更改通知。如果您希望在用戶輸入數據時實時更新,則應將綁定模式更改爲PropertyChangedwrite your own binding update behavior to update the bound source after a short delay

另外,作爲一個側面說明,你養了錯誤的屬性的PropertyChange通知您PrecioGram屬性:

public int PrecioGram 
{ 
    get { return _preciogram; } 
    set 
    { 
     _preciogram = value; 
     NotifyPropertyChanged("gramos"); // Incorrect property here 
    } 
} 
+0

中,「Data」與「Binding」相同,因爲我沒有得到該選項,如果我輸入該選項,它會將其標記爲錯誤? – hectormtnezg

+0

@hectormtnezg是的,對不起這個錯字,我修好了:) – Rachel