简体   繁体   English

WPF 列表框:如何绑定数据?

[英]WPF Listbox : How to bind data?

I have a listBox and add data to it like so:我有一个 listBox 并向其中添加数据,如下所示:

for (int x = 0; x < Orchestrator.Instance.getTopicCount(); x++)
{
   listTopics.Items.Add(Orchestrator.Instance.getTopic(x));
}

But I need to be able to do things like have text wrapping and divider lines, so I would like to do it the XAML.但是我需要能够做一些事情,比如有文本换行和分隔线,所以我想做 XAML。

Microsoft shows this:微软展示了这一点:

<TextBlock Width="248" Height="24" 
Text="{Binding ElementName=lbColor, Path=SelectedItem.Content, 
Mode=OneWay}" 
x:Name="tbSelectedColor" 
Background="{Binding ElementName=lbColor, Path=SelectedItem.Content, 
Mode=OneWay}"/>

But I don't really understand it.但我真的不明白。 Here is my XAML:这是我的 XAML:

<ListBox Height="261" HorizontalAlignment="Left" Margin="352,38,0,0" Name="listContent"     VerticalAlignment="Top" Width="391" Grid.Column="1" HorizontalContentAlignment="Left" VerticalContentAlignment="Center" MaxWidth="391" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>

How am I able to achieve what I want?我怎样才能实现我想要的? (Divider lines, text wrapping so I don't have to scroll horizontally, and data binding) (分隔线、文本换行,因此我不必水平滚动,以及数据绑定)

It seems that you will have to ascend the rather steep learning curve for how to use databinding in WPF first.看来您必须先爬上相当陡峭的学习曲线,才能在 WPF 中使用数据绑定。 Thereafter you should learn about DataTemplates and ItemTemplates to get the dividers and so forth.此后,您应该了解 DataTemplates 和 ItemTemplates 以获取分隔符等。 Try this to get you started试试这个让你开始

A book I can heartily recommend is WPF in Action from Manning.我可以衷心推荐的一本书是 Manning 的WPF in Action

You need to define the ItemTemplate of your ListBox.您需要定义 ListBox 的 ItemTemplate。 In your resources, add this:在您的资源中,添加以下内容:

<DataTemplate x:Key="myItemTemplate" TargetType="ListBoxItem">
    <TextBlock Text="{Binding}"/>
</DataTemplate>

Supposing that your getTopic method returns a string, otherwise use {Binding MyTopicProperty} where MyTopicProperty is a property in your Topic class.假设您的 getTopic 方法返回一个字符串,否则使用{Binding MyTopicProperty} ,其中MyTopicProperty是您的主题 class 中的一个属性。 Customize the TextBlock as you need.根据需要自定义 TextBlock。

Then use your ListBox like this:然后像这样使用您的 ListBox:

<ListBox ItemTemplate="{StaticResource myItemTemplate"/>

To specify the layout of each item, there are two different things you can change: the ItemContainerStyle, which provides the ControlTemplate used for each ListBoxItem, or the ItemTemplate, which provides the DataTemplate that is used to render each data item.要指定每个项目的布局,您可以更改两种不同的内容:ItemContainerStyle,它提供用于每个 ListBoxItem 的 ControlTemplate,或 ItemTemplate,它提供用于呈现每个数据项的 DataTemplate。 The ItemTemplate is simpler to use if you're just getting started and haven't gotten the hang of ControlTemplates yet.如果您刚刚开始并且还没有掌握 ControlTemplates 的窍门,那么 ItemTemplate 使用起来会更简单。

To get your text to wrap, there are two key things to do.要让你的文本换行,有两个关键的事情要做。 Turn off the default horizontal scrolling of ListBox (which you got already), and set the TextWrapping property of your TextBlock to Wrap.关闭 ListBox 的默认水平滚动(您已经获得),并将 TextBlock 的 TextWrapping 属性设置为 Wrap。 To get to the TextBlock you need to define it in your ItemTemplate.要访问 TextBlock,您需要在 ItemTemplate 中定义它。 Here's a simple example of the template declared inline, though you could also pull it out as a Resource.这是声明为内联的模板的一个简单示例,尽管您也可以将其作为资源拉出。 For the dividing line I used the simplest approach of a Border with only a bottom black line.对于分割线,我使用了最简单的边框方法,只有底部的黑线。

<ListBox x:Name="listContent" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderThickness="0,0,0,1" BorderBrush="Black">
                <TextBlock Text="{Binding}" TextWrapping="Wrap"/>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

here is an example how to bind listbox with RSS feed with DataTemplate:这是一个如何使用 DataTemplate 将列表框与 RSS 提要绑定的示例:

<UserControl.Resources>
        <XmlDataProvider x:Key ="DataRSS" XPath="//item" Source="http://rss.feedsportal.com/c/629/f/502199/index.rss">< /XmlDataProvider>
    </UserControl.Resources>

<StackPanel Orientation="Horizontal"  HorizontalAlignment="Center">

            <ListBox  ItemsSource="{Binding Source={StaticResource DataRSS}}"  Height="516" Margin="0,0,32,0" Background="{x:Null}" BorderBrush="#FF627DAE">
                <ListBox.ItemTemplate >
                    <DataTemplate >
                        <Grid Width="400" Height="100"  >                                

                            <Image Source="{Binding XPath=enclosure/@url}" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"  />
                            <TextBlock TextWrapping="Wrap" Text="{Binding XPath=title}" FontWeight="Bold" Grid.Column="2"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
</grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM