【问题标题】:How to display my TextBlock of ListBox HORIZONTALLY in RSS Feed WPF code如何在 RSS Feed WPF 代码中水平显示 ListBox 的 TextBlock
【发布时间】:2017-12-04 04:01:27
【问题描述】:

我知道这很简单,但我花了很多时间来水平显示我的列表。我什至将 make StackPanel 方向设置为“水平”,但都是徒劳的。有人能帮忙吗?我真的很感激。

<Window x:Class="RssFeed_Wpf.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:RssFeed_Wpf"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <XmlDataProvider x:Key="DataRss" XPath="//item" Source="http://www.abc.net.au/news/feed/52278/rss.xmlhttp://www.abc.net.au/news/feed/45910/rss.xml">
    </XmlDataProvider> 
</Window.Resources>
    <ListBox ItemsSource="{Binding Source = {StaticResource DataRss}}" Background="Black" HorizontalContentAlignment="Left" BorderBrush="Transparent">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding XPath=title}" FontWeight="Bold" Name="txtScrolling" FontFamily="calibri"  Foreground="#f4b042" Height="20pt">
                    </TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
 </Window>

你可以在这里看到,列表是垂直显示的,但我想要水平显示:

【问题讨论】:

标签: c# wpf xaml listbox rss


【解决方案1】:

因为您的 StackPanel 在 DataTemplate 中,所以为 ListBox 中的每个项目创建了一个 StackPanel。要更改ListBox 的容器,您需要将其设置为ItemsPanel

<ListBox >
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    相关资源
    最近更新 更多