简体   繁体   中英

WP7: Listbox item template doesn't work when edited?

**UPDATED**

Just something quick, hope you guys can help me but i'm having this problem where I open up my wp7 project in blend and i edit the listbox item template and i finish it but. I save everything and go back to VS2010 for Windows phone and hit debug but i look at the phone and i have no items showing up at all. The listbox is just blank.

Code:

<ListBox  toolkit:TiltEffect.IsTiltEnabled="True" x:Name="ListBox1" FontSize="42.667" FontFamily="Segoe WP SemiLight" IsSynchronizedWithCurrentItem="False" d:LayoutOverrides="VerticalAlignment">
    <ListBox.ItemTemplate>
       <DataTemplate>
          <StackPanel x:Name="sp">           
    <toolkit:ContextMenuService.ContextMenu>
   <toolkit:ContextMenu IsZoomEnabled="False" > 
  <toolkit:MenuItem Header="Delete" Click="Delete_Click" Name="MenuItem1" />
  <toolkit:MenuItem Header="Edit" Click="Edit_Click"/>
    <toolkit:MenuItem Header="View" Click="View_Click"/>
  <toolkit:MenuItem Header="Share.." Click="Share_Click"/>
</toolkit:ContextMenu>

Quick Brief The app I'm making is a simple note app which saves notes in to a folder in the isolated storage. It successfully retrieves the items but i just want to make it so that it has the title and a brief description. This is all in one item. I've got to that point and the 2 textblocks have ="{Binding}" this basically just adds the title I'm assuming but i also added the ="{Binding}" to the second textblock so its basically showing the title for both of them. Is there a way to bind it to a specific item? like the second textblock, how can i bind that so that it shows 1st 12 characters inside a text file so basically it just shows the title and a brief description?

Maybe you have designtime-only data? In case you're using Mvvm Light DataService approach, you define 2 DataServices: one for designtime and another for realtime.

Just random assuming. it would be nice to see some sample.

UPD: you posted wrong code, this one is about ContextMenu. I dont see binding there.

But again, generally talking, there shouldnt be any problems. You just deserialize data into model, say

public class Note
{
public string Name {get; set; }
public string Content; {get; set; }
}

And then you have List (or even ObservableCollection if you want realtime changes like renaming). And then you just bind

<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Content}"/>

If you want to have a strict limit for Content/Description of 12 characters, you can add either Converter and take only 12 first characters, or introduce new property

class Note 
{
***
public string Description { get { return Content.Substring(0, 12); } }
}

UPD2: Ok, lets start from the very beginning. First, MVVM is recommended pattern for Wp7 applications. I believe, you can google info about it yourself, but here are the most important parts:

  • Model. Keeps your data (in your case, it is notes names and description).
  • ViewModel. This is abstract view. You have all logic here, you have all data prepared to be rendered here, but VM have no idea how to render data. In your case, a list of notes would be here.
  • View. Here is description of your ui. In your case, ListBox would be here.

    1. So, first, make a new project, then use NuGet to install latest Mvvm Light (for example). After installing, you should see folders for viewmodels and models.

    2. Create new class Note, like i described before. It would be your model.

    3. Now, go to viewmodel. In a constructor, add a list of Notes there, call it ListOfNotes. Add manually several items to the list and initialize them (add some random values for Names and Contents fields).

    4. Now, go to view. In the top of the file, there should be something like DataContext = "{Binding MainViewModel, Source={StaticResource ViewModelLocator}}". Inside of the view, add ListBox. It should be something like

        <ListBox ItemsSource="{Binding ListOfNotes}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock Text="{Binding Name}" /> <TextBlock Text="{Binding Content}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

So, what would happen now. When you would run your app, your view would be initialized. It would get MainViewModel (because is it set as DataContext, in step 4). In the constructor of MainViewModel, a ListOfNotes would be initialized (see step 3). Then, when page would load ListBox, it would try to find ListOfNotes inside of DataContext (MainViewModel in our case). It should find your list of Notes, and then, every element of the ListBox would be associated with every element of your ListOfNotes. As it is described in DataTemplate, it would try to get Note.Name and Note.Content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM