0
这已被问过。我已经尝试了几件事,并阅读了很多帖子,但无法得到这个工作。会发生什么情况是窗口打开,我看不到listview
中的列。相反,我看到System.Windows.Controls.Gridview Columns=3
绑定到一个ListView
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();
}
啊污物。好。现在我看到了列,但ListView仍然是空的。你需要看代码隐藏和/或ViewModel吗? –
我会建议检查你的输出窗口,看看是否有任何数据绑定错误。如果您没有看到任何内容,那么您可能需要使用Snoop(如果您还没有,请使用伟大的工具)仔细查看您的ListView。不幸的是,我现在不在一个能够对ListView上的绑定进行任何更多疑难解答的地方。 –
我有一个数据绑定错误“在对象上找不到属性”。谢谢,至少是一个开始。 –