注意:上述问题与我的问题不同。
如何使一个列表视图显示多个表中的列? 这是我的列表视图
<ListView x:Name="lvac" HorizontalAlignment="Left" Height="380" Margin="23,180,0,0" ItemsSource="{Binding Path=''}" VerticalAlignment="Top" Width="949">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Date" DisplayMemberBinding="{Binding Path=Dated}" />
<GridViewColumn Width="120" Header="Transaction Description" DisplayMemberBinding="{Binding Path=AreaLocation}" />
<GridViewColumn Width="100" Header="Amount In" DisplayMemberBinding="{Binding Path=Amountin}" />
<GridViewColumn Width="100" Header="Amount Out" DisplayMemberBinding="{Binding Path=Amountout}" />
<GridViewColumn Width="100" Header="Balance" DisplayMemberBinding="{Binding Path=Bbalance}" />
</GridView>
</ListView.View>
</ListView>
我列表视图的所有列都属于“BankAccount”,除了属于“银行”的最后一列。如何才能使这成为可能?谢谢
注意:这里我没有使用任何框架工作。
答案 0 :(得分:0)
您无法一次将单个网格绑定到多个项目源。您可能要做的是创建一个新类并在其中创建BankAccount和Bank对象,并将此新类的对象绑定到gridview。
答案 1 :(得分:0)
是的,这是可能的。答案是拥有一个适当的ViewModel,为ListView提供所有必要的信息。
请看一下这里的链接:
http://en.wikipedia.org/wiki/Model_View_ViewModel
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
MVVM是采用这种方式的正确方法。
答案 2 :(得分:0)
如果您使用 MVVM 模式(在WPF中非常常见),则必须在ViewModel中实现一些“传输”属性,以便以自定义方式在多个绑定之间交换数据
答案 3 :(得分:0)
ListView.ItemsSource
属性可以绑定到一个集合项。因此,没有直接的方法来实现这一目标。但是,您可以创建一个类,该类封装您要在ListView中显示的所有值。然后创建一个ObservableCollection并将其绑定到ListView.ItemSource。
注意:如果您遵循任何UI模式,您可以更清晰地实现此目的。但是,只是为了保持简单
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_dataItems = GetDataItems();
DataContext = this;
}
private ObservableCollection<Combination> GetDataItems()
{
//Here load all of your individual data structure
//Eg
var items = new List<Combination>
{
new Combination(new Bank {AccountNo = 112, AccountName = "somename"},
new Cheque {ChequeDate = DateTime.Now, ChequeNo = 234562}),
new Combination(new Bank {AccountNo = 132, AccountName = "name"},
new Cheque {ChequeDate = DateTime.Now, ChequeNo = 215562}),
new Combination(new Bank {AccountNo = 212, AccountName = "someother"},
new Cheque {ChequeDate = DateTime.Now, ChequeNo = 114562})
};
return new ObservableCollection<Combination>(items);
}
private ObservableCollection<Combination> _dataItems;
public ObservableCollection<Combination> DataItems
{
get { return _dataItems; }
set { _dataItems = value; }
}
}
public class Combination
{
public Combination(Bank bk, Cheque cq)
{
ChequeNo = cq.ChequeNo;
ChequeDate = cq.ChequeDate;
AccountName = bk.AccountName;
AccountNo = bk.AccountNo;
}
public int ChequeNo { get; set; }
public DateTime ChequeDate { get; set; }
public int AccountNo { get; set; }
public String AccountName { get; set; }
}
public class Cheque
{
public int ChequeNo { get; set; }
public DateTime ChequeDate { get; set; }
}
public class Bank
{
public int AccountNo { get; set; }
public String AccountName { get; set; }
}
最后将其绑定到ListView
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView x:Name="lvac" HorizontalAlignment="Left" ItemsSource="{Binding Path=DataItems}" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="Cheque Num" DisplayMemberBinding="{Binding Path=ChequeNo}" />
<GridViewColumn Width="100" Header="Ch. date" DisplayMemberBinding="{Binding Path=ChequeDate}" />
<GridViewColumn Width="100" Header="Account Name" DisplayMemberBinding="{Binding Path=AccountName}" />
<GridViewColumn Width="100" Header="Account No" DisplayMemberBinding="{Binding Path=AccountNo}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
所以结果将如下所示