Windows phone 8 ListBox

use*_*588 2 c# wpf xaml listbox windows-phone-8

我试图为我的一个学校项目做一个应用程序,它几乎结束但现在最后我有一个问题.

我有一个列表框,我有一个字符串数组,可以在该listBox中加载文本.很少有字符串很长,文字离开屏幕.没有文字包装或类似的东西?请告诉我如何将文本转到第二行而不是溢出屏幕?

这是我的测试代码,类似于我的项目.它具有相同的ListBox和两个带有长字的字符串.

XAML:

<phone:PhoneApplicationPage
x:Class="Test_Listbox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>



    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"         Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        <ListBox Name="ListboxTest"></ListBox>

    </Grid>


    </Grid>

</phone:PhoneApplicationPage>
Run Code Online (Sandbox Code Playgroud)

我的cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;


namespace Test_Listbox
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        ListboxTest.Items.Add(" List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1 List box 1");
        ListboxTest.Items.Add(" List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2 List box 2");

    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 6

我建议使用模板和绑定.

例如:

<ListBox Name="ListboxTest" ItemsSource={Binding}>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

并在您的代码后面:

List<String> ItemsListProperty{ set; get; }

public MainPage()
{
    InitializeComponent();

    this.DataContext = ItemsListProperty;
}
Run Code Online (Sandbox Code Playgroud)

您需要定义ItemsListProperty,但它比直接向ListBox控件添加项目更好.

  • 没问题.询问是如何得到答案的.您需要定义属性ItemsListProperty(或您决定命名的任何内容).这是一个[关于.NET属性的教程](http://msdn.microsoft.com/en-us/library/aa288470(v = vs.71).aspx). (4认同)