简体   繁体   English

WP7:列表框项目模板在编辑后不起作用?

[英]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. 很快,希望你们能为我提供帮助,但是我遇到了这个问题,我在混合状态下打开wp7项目,然后编辑列表框项目模板,但完成了。 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. 我保存了所有内容,然后返回Windows Phone的VS2010,然后单击“调试”,但我看了一下手机,没有任何显示的东西。 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. 我已经到了这一点,两个文本块都带有=“ {Binding}”,这基本上只是添加了我假设的标题,但我还向第二个文本块添加了=“ {Binding}”,所以它基本上显示了他们都。 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? 像第二个文本块一样,我该如何绑定它,使其在文本文件中显示第1个12个字符,因此基本上它仅显示标题和简短描述?

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. 如果您使用的是Mvvm Light DataService方法,则定义两个DataService:一个用于设计时,另一个用于实时。

Just random assuming. 只是随机假设。 it would be nice to see some sample. 很高兴看到一些样本。

UPD: you posted wrong code, this one is about ContextMenu. UPD:您发布了错误的代码,这是关于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). 然后您有了List(如果您想实时更改(如重命名),甚至还有ObservableCollection)。 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 如果要对内容/描述的12个字符进行严格限制,则可以添加Converter并仅输入12个首字符,或引入新属性

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

UPD2: Ok, lets start from the very beginning. UPD2:好的,让我们从头开始。 First, MVVM is recommended pattern for Wp7 applications. 首先,对于Wp7应用程序,推荐使用MVVM模式。 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. 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. 您这里拥有所有逻辑,已经准备好在此处呈现所有数据,但是VM不知道如何呈现数据。 In your case, a list of notes would be here. 在您的情况下,注释列表将在此处。
  • View. 视图。 Here is description of your ui. 这是您的用户界面的说明。 In your case, ListBox would be here. 就您而言,ListBox会在这里。

    1. So, first, make a new project, then use NuGet to install latest Mvvm Light (for example). 因此,首先创建一个新项目,然后使用NuGet安装最新的Mvvm Light(例如)。 After installing, you should see folders for viewmodels and models. 安装后,您应该看到用于视图模型和模型的文件夹。

    2. Create new class Note, like i described before. 像我之前描述的那样创建新的类Note。 It would be your model. 这将是您的模型。

    3. Now, go to viewmodel. 现在,转到viewmodel。 In a constructor, add a list of Notes there, call it ListOfNotes. 在构造函数中,在此处添加一个Notes列表,将其称为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}}". 在文件顶部,应该有类似DataContext =“ {Binding MainViewModel,Source = {StaticResource ViewModelLocator}}”的名称。 Inside of the view, add ListBox. 在视图内部,添加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). 它将得到MainViewModel(因为在步骤4中将其设置为DataContext)。 In the constructor of MainViewModel, a ListOfNotes would be initialized (see step 3). 在MainViewModel的构造函数中,将初始化ListOfNotes(请参阅步骤3)。 Then, when page would load ListBox, it would try to find ListOfNotes inside of DataContext (MainViewModel in our case). 然后,当页面加载ListBox时,它将尝试在DataContext(在我们的情况下为MainViewModel)中查找ListOfNotes。 It should find your list of Notes, and then, every element of the ListBox would be associated with every element of your ListOfNotes. 它应该找到您的Notes列表,然后,ListBox的每个元素都将与ListOfNotes的每个元素相关联。 As it is described in DataTemplate, it would try to get Note.Name and Note.Content. 如DataTemplate中所述,它将尝试获取Note.Name和Note.Content。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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