WPF中的源绑定,如Asp.Net

时间:2015-06-17 06:24:31

标签: c# wpf xaml mvvm

我有一个事务表来存储一天发生的所有交易。我写了一个查询

select CurrencyCode,TransactionCode,TransactionAmount,COUNT(TransactionCode) as 
[No. Of Trans] from TransactionDetails where CAST(CurrentTime as date)=CAST(GETDATE()as date)
group by TransactionCode, CurrencyCode,TransactionAmount order by CurrencyCode

我得到了这样的出局

CurrencyCode      TransactionCode      TransactionAmount   No.OfTrans
  ARS                 SCN                   12                 1
  AWG                 BNT                   12                 1
  BAM                 BNT                   12                 1
  BND                 BNT                   7                  1
  BWP                 BTC                   45                 1
  EUR                 BCN                   6.6                1
  EUR                 BNT                   90                 1
  EUR                 BNT                   200                1
  JPY                 BNT                   10000              2
  VND                 BNT                   600                1
  VND                 BNT                   800                1
  VND                 BNT                   10000000           1

但是根据我的要求,我想在报告目的的页面上显示这样的内容

                       ARS
BNT        0            0
BCN        0            0
SCN        1            12
BTC        0            0
STC        0            0
SNT        0            0
                     -------
Total Balance  --->    70000
++++++++++++++++++++++++++++++++++++
                       VND
BNT        3           10001400
BCN        0            0
SCN        0            0
BTC        0            0
STC        0            0
SNT        0            0
                     -------
Total Balance  --->    17090
++++++++++++++++++++++++++++++++++++

有什么方法可以像asp.net那样进行源代码绑定。在网格中设计模式并根据我的货币代码应用循环。这样我就能达到我的要求。

1 个答案:

答案 0 :(得分:1)

首先,你需要在listview中创建一个代表f.e行的类:

public class CurrencyClass
{
       public string CurrencyCode {get;set;}
       public string TransactionCode {get;set;}
       public string TransactionAmount {get;set;}
       public string NoOfTrans {get;set;}
}

然后让我们创建一个DataContext类,它拥有一个关于listview的集合和方法。

public class CurrencyViewModel
{
       public CurrencyViewModel()
       {
              ListOfCurrency = new ObservableCollection<CurrencyClass>();
       }

       public ObservableCollection<CurrencyClass> ListOfCurrency;
}

因为list是ObservableCollection的类型,所以如果它的集合发生变化,它会自动通知你的ListView。

现在我们需要创建一个代表你的表的ListView:

<ListView Name="CurrencyListView">
                <ListView.View>
                    <GridView>
                          <GridViewColumn Width="200" DisplayMemberBinding="{Binding TransactionCode}" Header="" />
                          <GridViewColumn Width="200" DisplayMemberBinding="{Binding NoOfTrans}" Header="" />
                          <GridViewColumn Width="200" DisplayMemberBinding="{Binding TransactionAmount}" Header="" />
                    </GridView>
                </ListView.View>
            </ListView>

现在让我们将ListView和ViewModel绑定在一起,例如在Window构造函数中:

CurrencyViewModel currencyViewModel = new CurrencyViewModel();
CurrencyListView.DataContext = currencyViewModel;
CurrencyListView.ItemsSource = currencyViewModel.CurrencyListView;

现在您需要做的就是将项目添加到currencyViewModel.CurrencyListView,它将显示在ListView中。请记住为您的目的更改GridViewColumn的值。