【问题标题】:Wrap text inside Listbox item在列表框项内换行
【发布时间】:2016-09-26 21:06:59
【问题描述】:

问题


我有一个绑定到 ObservableCollectionListbox

Polls = new ObservableCollection<string>();

DataSet LoadAllPolls = _publisher.GetQuizFeatures(Global.gEpisodeWS.Id);

foreach (DataRow item in LoadAllPolls.Tables[0].Rows)
{
    Polls.Add(item["Description"].ToString());
}

这是 XAML:

<ListBox x:Name="lb_polls" Background="#ececec" BorderBrush="Transparent" SelectedItem="{Binding SelectedPoll}" ItemsSource="{Binding Polls}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="Height" Value="110"/>
            <Setter Property="Width" Value="200"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#858585" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#858585" />
        <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#858585"/>
    </ListBox.Resources>
</ListBox>

现在我希望将添加到 ListBox 项的字符串换行。我该怎么做?

【问题讨论】:

    标签: c# wpf listbox


    【解决方案1】:

    这很简单,但我不知道为什么你的代码不起作用,这是我的xaml

    <Grid Width="600" Height="Auto" >
            <ListBox  ItemsSource="{Binding Polls}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="Height" Value="110"/>
                        <Setter Property="Width" Value="200"/>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    

    绑定到 ObservableCollection 投票并且工作正常。

           private void LoadPoll()
            {
                for (int i = 1; i <= 3; i++)
                {
                    Polls.Add("Poll" + i.ToString());
                }
            }
    
            private ObservableCollection<string> _Polls = new ObservableCollection<string>();
            public ObservableCollection<string> Polls
            {
                get { return _Polls; }
                set
                {
                    _Polls = value;
                }
            }     
    

    【讨论】:

      【解决方案2】:

      添加这个ItemTemplate:

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

      【讨论】:

      • 您可能还需要将水平滚动条设置为“从不”,因为我相信 ListBox 的默认值是“自动”,因此它只会滚动而不是换行。不过可能是错的。
      • @Clemens 我现在只能在列表中看到 1 项,而不是全部 3 项。
      • 对我有用,不过,与您的 XAML 完全一样。不需要设置任何 Scrollbar 属性,因为 ListBoxItems 的 Width 由 ItemContainerStyle 显式设置。
      • @Clemens 该项目被我移到了中间,因此不再显示。
      • 那么您的 XAML 或代码中一定有一些您没有向我们展示的内容。
      猜你喜欢
      • 2010-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多