Display properties of the ListBox.ItemsSource

2019-01-12 10:00发布

I am new to WPF. I have a ListBox that has its ItemSource set to a instance of WorkItemCollection. (A collection of WorkItem objects.)

When the list is displayed it only displays the type of each object (Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem). Is there a way to make the list display WorkItem.Title?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-12 10:39

You have two options.

The simplest method is to set the DisplayMemberPath property of your ListBox to "Title".

If you want to set not only what gets displayed, but the type of control that is used to display it, then you would set the ListBox's ItemTemplate.

For what your goal is, I would recommend the first option.

查看更多
混吃等死
3楼-- · 2019-01-12 10:53

You can set a DataTemplate on the ItemTemplate property of the ListBox:

<ListBox ItemSource="{Binding}">
  <ListBox.ItemTemplate>
    <DataTemplate DataType="tfs:WorkItem">
      <StackPanel>
        <TextBlock Text="{Binding Title}" />
        <!-- Others -->
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>
查看更多
登录 后发表回答