如何在RSS Feed WPF代码中显示ListBox HORIZONTALLY的TextBlock

时间:2017-12-04 04:01:27

标签: c# wpf xaml listbox rss

我知道这很简单,但我花了很多时间水平显示我的列表。我甚至将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>

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

Screenshot

1 个答案:

答案 0 :(得分:1)

由于StackPanel位于DataTemplate中,因此为StackPanel中的每个项目创建了ListBox。要更改ListBox的容器,您需要将其设置为ItemsPanel

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