2013-11-21 62 views
0

我正在做wpf生成一些基於日期範圍的列。我怎麼能設法得到數據基於我的sqlite數據(shiftdate)和動態添加項目到列表視圖?如何在日期範圍內添加listView項目基礎?

我想是這樣的: enter image description here

我的SQLite的數據使用SQLite觀衆: enter image description here

到目前爲止我的代碼工作僅在第一列;

在我的XAML

<Window x:Class="SaranacHUD.AdminFeedback" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:SaranacHUD" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    Title="Feedback" Height="800" Width="1200" WindowStartupLocation="CenterScreen" Topmost="True"> 

<Window.Resources> 
    <local:ViewModel x:Key="ViewModel" /> 
    <Style TargetType="{x:Type ListViewItem}"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
    </Style> 
</Window.Resources> 

<Grid DataContext="{StaticResource ViewModel}"> 
    <ListView Name="listView1" Margin="10,42,10,10" ItemsSource="{Binding CollectionAccount}" IsSynchronizedWithCurrentItem="True"> 
     <ListView.View> 
      <GridView x:Name="gridViewAll"> 
      </GridView> 
     </ListView.View> 
    </ListView> 

    <DatePicker Name="DatePicker1" HorizontalAlignment="Left" Height="27" Margin="10,10,0,0" VerticalAlignment="Top" Width="106"/> 
    <DatePicker Name="DatePicker2" Height="27" Margin="151,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="113"/> 
    <Button Name="buttonGenerate" Content="Generate" Height="30" Margin="269,7,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="156"/> 
    <Label Content="TO" HorizontalAlignment="Left" Margin="121,10,0,0" VerticalAlignment="Top" Height="30"/> 
</Grid> 

代碼背後:

public partial class AdminFeedback : Window 
{ 
    ViewModel _ViewModel = null; 

    string year1; 
    string month1; 
    string day1; 

    string year2; 
    string month2; 
    string day2; 

    public AdminFeedback() 
    { 
     InitializeComponent(); 

     _ViewModel = Resources["ViewModel"] as ViewModel; 


     Loaded += (Ev, ev) => 
     { 
      DatePicker1.SelectedDate = DateTime.Today; 
      DatePicker2.SelectedDate = DateTime.Today; 
     }; 

     DatePicker1.SelectedDateChanged += (Ev, ev) => 
     { 
      year1 = DatePicker1.SelectedDate.Value.ToString("yyyy"); 
      month1 = DatePicker1.SelectedDate.Value.ToString("MM"); 
      day1 = DatePicker1.SelectedDate.Value.ToString("dd"); 
     }; 

     DatePicker2.SelectedDateChanged += (Ev, ev) => 
     { 
      year2 = DatePicker2.SelectedDate.Value.ToString("yyyy"); 
      month2 = DatePicker2.SelectedDate.Value.ToString("MM"); 
      day2 = DatePicker2.SelectedDate.Value.ToString("dd"); 
     }; 

     buttonGenerate.Click += (Ev, ev) => 
     { 
      CreateColumns(); 
     }; 
    } 

    private void CreateColumns() 
    { 
     gridViewAll.Columns.Clear(); 
     GridViewColumn UserName = new GridViewColumn(); 
     UserName.Header = "UserName"; 
     UserName.Width = 150; 
     UserName.DisplayMemberBinding = new Binding("UserName"); 
     gridViewAll.Columns.Add(UserName); 



     var startDate = new DateTime(int.Parse(year1), int.Parse(month1), int.Parse(day1)); 
     var endDate = new DateTime(int.Parse(year2), int.Parse(month2), int.Parse(day2)); 

     var tmp = startDate; 
     while (tmp <= endDate) 
     { 
      GridViewColumn _gridViewColumn = new GridViewColumn(); 
      _gridViewColumn.Header = tmp.ToString("MM/dd"); 
      _gridViewColumn.Width = 50; 
      _gridViewColumn.DisplayMemberBinding = new Binding("GridViewColumn"); 
      gridViewAll.Columns.Add(_gridViewColumn); 
      tmp = tmp.AddDays(1); 
     } 

     _ViewModel.FillFeedback(); 

    } 
} 

在我的ViewModel

public void FillFeedback() 
    { 
     sqliteConMain.connection.Close(); 
     sqliteConMain.connection.Open(); 

     var _command = sqliteConMain.connection.CreateCommand(); 
     var query = string.Format("SELECT * FROM tblUser Where Site = '" + "1880" + "' COLLATE NOCASE"); 
     string commandText = query; 

     var _dataAdapter = new SQLiteDataAdapter(commandText, sqliteConMain.connection); 

     DataSet _dataSet = new DataSet(); 
     DataTable _dataTable = new DataTable(); 

     _dataSet.Reset(); 
     _dataAdapter.Fill(_dataSet); 

     sqliteConMain.connection.Close(); 

     _dataTable = _dataSet.Tables[0]; 
     var rows = _dataTable.Rows; 

     CollectionAccount.Clear(); 
     foreach (DataRow row in rows) 
     { 
      var userName = row.ItemArray[0] as string; 
      string teamNumber = row.ItemArray[1] as string; 
      string groupName = row.ItemArray[2] as string; 
      string site = row.ItemArray[3] as string; 
      string switchRole = row.ItemArray[4] as string; 

      var UserDataProperties = new UserDataProperties 
      { 
       UserName = userName, 
       TeamNumber = teamNumber, 
       GroupName = groupName, 

      }; 

      _collectionAccount.Add(UserDataProperties); 
     } 
    } 

回答

0

我建議你看看這個:ListView DataBinding

正如頁面末尾所述,您將需要創建一個項目模板以正確顯示來自collectionAccout的信息。該視圖將如何知道如何呈現UserDataProperties?

嘗試這樣:

<ListView.ItemTemplate> 
     <DataTemplate> 
      <WrapPanel> 
       <TextBlock Text="UserDataProperties: " /> 
       <TextBlock Text="{Binding UserName}" /> 
       <TextBlock Text="{Binding Teamnumber}" /> 
       <TextBlock Text="{Binding GroupName}" /> 

      </WrapPanel> 
     </DataTemplate> 
    </ListView.ItemTemplate> 

當然這需要一些調整,直到它看起來就像你想讓它。