以前有人问过这个问题。我已经尝试了几件事并阅读了很多帖子,但无法使其正常工作。发生的情况是窗口打开,我看不到listview
. 相反,我看到System.Windows.Controls.Gridview Columns=3
XAML 和代码隐藏。ViewModel
如有必要,我可以发布
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();
}