2013-10-20 25 views
0

基本上我正在做這個簡單的筆記應用程序,我有一個列表框。當用戶通過點擊保存按鈕添加註釋時,註釋保存爲.txt文件,並在列表框中顯示註釋。所以現在如果用戶想要進行更改等,他們可以點擊列表框項目,這將是他們保存的筆記的名稱。我想要做的事情基本上是當用戶點擊保存時,它會保存並在列表框中顯示該筆記的名字,例如,注意名稱是test.txt,然後在列表框中出現test.txt。只是在列表框中出現的項目名稱下,我如何添加一種子項目?例如就像wp7中的郵件一樣。它有電子郵件的主題和顯示前12個單詞的子項目。我怎樣才能做到這一點與我的應用程序?所以我只得到筆記的名字。我希望它顯示名稱和消息下的前14個單詞。我希望這是有道理的。Wp7列表框子項目需要幫助

我的代碼洙遠:

這基本上增加了內部myNote項目folderto列表框,使其顯示保存的筆記。

Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication() 
    myIsolatedStorage.CreateDirectory("myNote") 
    Dim directory As String = "./myNote/*.*" 
    Dim filenames As String() = myIsolatedStorage.GetFileNames(directory) 
    ListBox1.ItemsSource = filenames 

這是列表框

<ListBox HorizontalAlignment="Left" Margin="8,8,0,8" x:Name="ListBox1" Width="440" SelectionMode="Single" FontSize="32" FontFamily="Segoe WP SemiLight" RenderTransformOrigin="0.5,0.5" Grid.Row="2" Style="{StaticResource ListBoxStyle1}" Foreground="White" BorderBrush="#00000000" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ItemsPanel="{StaticResource ItemsPanelTemplate1}" /> 

任何的XML?謝謝!

回答

0

要在每個列表框項目的文件標題下添加一些文本,您需要爲您的列表框定義一個新的ItemTemplate。通過定義您自己的ItemTemplate,您將能夠精確選擇每個列表框項目的顯示方式。

這裏是一個小樣本與所提供的Listbox,是非常簡單的模板(即包含在DataTemplate節的一切),它給你通過改變顏色,字體進行定製等等...

在您的XAML頁面:

 <ListBox HorizontalAlignment="Left" Margin="8,8,0,8" x:Name="ListBox1" 
       Width="440" SelectionMode="Single" 
       FontSize="32" FontFamily="Segoe WP SemiLight" 
       RenderTransformOrigin="0.5,0.5" Grid.Row="2" 
       Style="{StaticResource ListBoxStyle1}" 
       Foreground="White" BorderBrush="#00000000" 
       ItemContainerStyle="{StaticResource ListBoxItemStyle1}" 
       ItemsPanel="{StaticResource ItemsPanelTemplate1}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock Text="{Binding FileName}" FontWeight="Bold"></TextBlock> 
         <TextBlock Text="{Binding Content}"></TextBlock> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

然後,你需要分配給ItemsSource一種新型的List。而不只是分配一個文件名List(這是String一個List),你必須要提供的東西,如List

Public Class NewListItem 
    Public Property FileName() As String 
     Get 
      Return m_FileName 
     End Get 
     Set 
      m_FileName = Value 
     End Set 
    End Property 
    Private m_FileName As String 
    Public Property Content() As String 
     Get 
      Return m_Content 
     End Get 
     Set 
      m_Content = Value 
     End Set 
    End Property 
    Private m_Content As String 
End Class 

因此,基本上,它只是包含2個屬性的數據結構:FileNameContent

最後,你需要通過你的文件名List迭代,並用適當的值填充新特性來構建新型的List(當然,這只是僞代碼):

Dim newList As New List(Of NewListItem)() 
For Each file As var In filenames 
    Dim item As New NewFileItem() 
    item = file 
    ' open the file the way you want 
    ' extract the 14 first characters the way you want 
    Dim content As String = InlineAssignHelper(item.Content, content) 
    newList.Add(item) 
Next 
ListBox1.ItemsSource = filenames 
+0

感謝您的答覆但我有一個問題。 在本節: 昏暗newList作爲新的列表(OF NewListItem)() 每個文件作爲變種在文件名 昏暗項作爲新NewFileItem() 項=文件 昏暗內容作爲字符串= InlineAssignHelper(項目。內容,內容) newList.Add(item) Next ListBox1。ItemsSource =文件名 它說:「爲每個文件作爲VAR在文件名」 我得到一個錯誤,說「類型」VAR「未定義」。我無法測試代碼,這看起來真的很好?有什麼想法爲什麼我得到這個? –

+0

我不是VB.NET專家,但我認爲你應該將「For Each file As var In filenames」改爲「For Each File As String In filenames」,並且不要忘記我只提供了僞代碼(它不是意在編譯,只是爲了方便理解)。 「文件名」列表應該是在你的問題提供的(昏暗的文件名作爲字符串()= myIsolatedStorage.GetFileNames(目錄))。希望這可以幫助! – AirL

+0

您好,感謝那可是一個問題導致了另一個:(現在它爲: 昏暗的項目作爲新NewFileItem() 項目=文件 「打開文件你想 的方式」提取14個前幾個字符你的方式要 昏暗的內容 NewFileItem沒有定義,並且InlineAssignHelper未聲明的字符串= InlineAssignHelper(item.Content,內容) newList.Add(項目)。 –