2012-10-31 60 views
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(); 
     } 

回答

0

上述XAML中插入GridView控件對象ListView控件的內容。因此它覆蓋了ItemsSource。你要分配的GridView控件爲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> 
+0

啊污物。好。現在我看到了列,但ListView仍然是空的。你需要看代碼隱藏和/或ViewModel嗎? –

+0

我會建議檢查你的輸出窗口,看看是否有任何數據綁定錯誤。如果您沒有看到任何內容,那麼您可能需要使用Snoop(如果您還沒有,請使用偉大的工具)仔細查看您的ListView。不幸的是,我現在不在一個能夠對ListView上的綁定進行任何更多疑難解答的地方。 –

+0

我有一個數據綁定錯誤「在對象上找不到屬性」。謝謝,至少是一個開始。 –