绑定到 ListView [英] Binding To A ListView
问题描述
这已经被问过了.我尝试了几件事并阅读了很多帖子,但无法使其正常工作.发生的情况是窗口打开,但我在 listview
中看不到列.相反,我看到 System.Windows.Controls.Gridview Columns=3
This has been asked before. I have tried several things and read a lot of posts but can't get this working. What happens is the window opens and I don't see columns in the listview
. Instead I see System.Windows.Controls.Gridview Columns=3
XAML 和代码隐藏.如有必要,我可以发布 ViewModel
The XAML and code-behind. I can post the ViewModel
if necessary
XAML
<Window x:Class="DataBinding.LogFileWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataBinding"
Title="LogFileWindow" Height="556.183" Width="717.987">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<Grid>
<TabPanel Margin="12,131,128.6,12.4" Width="Auto" Grid.RowSpan="2">
<TabControl Margin="10,10,-270,-223.8">
<TabItem Header="Logging Log">
<ListView Name="LoggingListView" ItemsSource="{Binding LogEntries}">
<GridView>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Date}"></GridViewColumn>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=Time}"></GridViewColumn>
<GridViewColumn Header="Event" DisplayMemberBinding="{Binding Path=Event}"></GridViewColumn>
</GridView>
</ListView>
</TabItem>
<TabItem Header="Accounting Log">
<ListView Name="AccountingListView" ItemsSource="{Binding}" Margin="0,0,-14.6,38.2"></ListView>
</TabItem>
<TabItem Header="NVRAM Log">
<ListView Name="NVRAMListView" ItemsSource="{Binding}"></ListView>
</TabItem>
</TabControl>
</TabPanel>
</Grid>
C#
public MainWindow()
{
//create a new logging log file
LoggingLog logfile = new LoggingLog();
//add some entries
// these are not real dates and times but that doesn't matter much.
for (int i = 0; i < 20; i++)
{
LogFileEntry le = new LogFileEntry();
le.Date = String.Format("Date{0}", i);
le.Time = String.Format("Time{0}", i);
le.Event = String.Format("Event{0}", i);
logfile.LogEntryList.Add(le);
}
//now load the viewmodel
LogViewModel logviewmodel = new LogViewModel();
foreach (LogFileEntry le in logfile.LogEntryList)
{
logviewmodel.Add(new LogEntryViewModel(le));
}
//now set the datacontext on the window
LogFileWindow lfwindow = new LogFileWindow();
lfwindow.LogEntries = logviewmodel;
lfwindow.LoggingListView.DataContext = logviewmodel;
InitializeComponent();
lfwindow.Show();
}
推荐答案
您上面的 XAML 将 GridView 对象插入为 ListView 的内容.所以它覆盖了 ItemsSource.您想将 GridView 指定为 ListView 的视图:
Your XAML above inserts the GridView object as the content of the ListView. So it overrides the ItemsSource. you want to assign the GridView as the View of the ListView:
<ListView Name="LoggingListView" ItemsSource="{Binding LogEntries}">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Date}"></GridViewColumn>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=Time}"></GridViewColumn>
<GridViewColumn Header="Event" DisplayMemberBinding="{Binding Path=Event}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
这篇关于绑定到 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!