0

我有一个ListBox已绑定到ObservableCollection我的自定义租户对象的一个​​,它具有包括 startDate 和 endDate 的属性。

但是,我希望ListBox显示每个ListBoxItem表单:

01/01/2001 - 22/12/2012

所以这是两个绑定属性和中间的一个子字符串。

如何ItemSource以这种方式格式化输出?我明白DisplayMemberPath指向我想要的属性,但我需要两个DisplayMemberPaths,对吗?

谢谢你的帮助。

4

2 回答 2

2

您可以制作自定义 ItemTemplate 来实现此外观:

<ListBox ItemsSource="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding StartDate}"/><Run Text=" - " /><Run Text="{Binding EndDate}" />
            </TextBlock>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
于 2013-02-04T16:10:02.977 回答
0

DisplayMemberPathItemTemplate只是说应该只是一个 TextBlock的快捷方式,它的 Text 绑定到 DisplayMemberPath 项。

如果您希望ItemTemplate更详细,可以覆盖该ItemTemplate属性。

例如,

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBox>
            <TextBlock.Text>
                <MultiBinding StringFormat="{0:M/d/yy} - {1:M/d/yy}">
                    <Binding Path="StartDate " />
                    <Binding Path="EndDate" />
                 </MultiBinding>
             </TextBlock.Text>
        </TextBox>
    </DataTemplate>
</ListBox.ItemTemplate>
于 2013-02-04T16:11:09.887 回答