开发者

WPF Listbox : How to bind data?

I have a listBox and add data to it like so:

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.

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:

<ListBox Height="261" HorizontalAlignment="Left" Margin="352,38,0,0" Name="lis开发者_运维知识库tContent"     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)


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. The ItemTemplate is simpler to use if you're just getting started and haven't gotten the hang of ControlTemplates yet.

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. To get to the TextBlock you need to define it in your 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>


It seems that you will have to ascend the rather steep learning curve for how to use databinding in WPF first. Thereafter you should learn about DataTemplates and ItemTemplates to get the dividers and so forth. Try this to get you started

A book I can heartily recommend is WPF in Action from Manning.


You need to define the ItemTemplate of your ListBox. 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. Customize the TextBlock as you need.

Then use your ListBox like this:

<ListBox ItemTemplate="{StaticResource myItemTemplate"/>


here is an example how to bind listbox with RSS feed with DataTemplate:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜