wpf Windows 手机 8 列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19774117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:00:38  来源:igfitidea点击:

Windows phone 8 ListBox

c#wpfxamlwindows-phone-8listbox

提问by user2919588

I`m trying to make an aplication for one of my school projects, and its almost over but now at the end I have a problem.

我正在尝试为我的一个学校项目申请,它快结束了,但现在最后我遇到了问题。

I have a listbox, and I have a string array that loads text in that listBox. Few Strings are long and text get out of the screen. There is no text wrap or something like that? Please tell me how can I make the text go to second line and not overflow out of the screen?

我有一个列表框,我有一个字符串数组,用于在该列表框中加载文本。很少有字符串很长并且文本会离开屏幕。没有文字换行或类似的东西?请告诉我如何使文本转到第二行而不溢出屏幕?

This is my test code, that is similar to my project. It has the same ListBox and two strings in it with long words.

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

xaml:

xml:

<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>

And my cs file:

还有我的 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");

    }
  }
}

回答by Mark MacDonnell

I recommend using a template and a binding.

我建议使用模板和绑定。

For instance:

例如:

<ListBox Name="ListboxTest" ItemsSource={Binding}>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and in your code behind:

并在您的代码后面:

List<String> ItemsListProperty{ set; get; }

public MainPage()
{
    InitializeComponent();

    this.DataContext = ItemsListProperty;
}

You'll need to define the ItemsListProperty, but it's better than adding items directly to the ListBox control.

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