如何在不受 ItemTemplate 影响的情况下向 ListBox 项添加分隔符
下面您可以看到视图和视图模型。 结果将是:“ABC”,每个字母的背景都是红色的。 我想在项目之间添加一个箭头但是我不希望箭头被涂成红色。 这意味着它应该是这样的:“A --> B --> C”,只有字母才会被红色着色,而箭头不会。 我可以使用转换器在文本属性上添加箭头,但它最终也会给箭头着色。
有什么想法吗?
Xaml:
<ListBox ItemsSource="{Binding MyArray}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Background="Red" Text="{Binding}" Margin="5"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
背后代码:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
MyArray = new ObservableCollection<string>();
MyArray.Add("A");
MyArray.Add("B");
MyArray.Add("C");
}
public ObservableCollection<string> MyArray { get; set; }
}
Below you can see the View and the ViewModel.
The result will be :" A B C " that the background of each letter is red.
I want to add an arrow between the items BUT I dont want the arrow to be colored by red.
That mean it should be like this : "A --> B -- > C" that ONLY the letters will be colored by red, and the arrows NOT.
I can use a Converter to add the arrow on the Text propertry, but it will end up color the arrow as well.
Any Idea ?
Xaml:
<ListBox ItemsSource="{Binding MyArray}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Background="Red" Text="{Binding}" Margin="5"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code Behind:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
MyArray = new ObservableCollection<string>();
MyArray.Add("A");
MyArray.Add("B");
MyArray.Add("C");
}
public ObservableCollection<string> MyArray { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以轻松地做到这一点:
或者,如果您确实必须将其保留在数据模板之外,请使用
ItemContainerStyle
并将新的Template
分配给ListBoxItem
,其中包含旁边的箭头ContentPresenter
项目模板所在的位置(这可能是一个好主意,因为这样您可以防止箭头显示为选中状态)。编辑:我会使用带有
PreviousData
绑定的附加箭头来解决这个问题,如果它为空,则它之前没有项目:You could easily do this:
Or if you really must keep it out of the data template use the
ItemContainerStyle
and assign a newTemplate
to theListBoxItem
which contains an arrow next to theContentPresenter
where the item template will be (this might be a good idea as you then can prevent the arrow from appeearing selected).Edit: I would approach the issue with the additional arrow with a
PreviousData
binding, if it is null there is no item before it:如果您知道集合中的最大项目数,则可以将
ListBox
的AlternationCount
设置为大于集合中项目数的数字,然后使用DataTrigger
确定分隔符项目的可见性或文本。编辑
正如HB在他的回答中指出的那样,您还可以将
DataTrigger
基于RelativeSource.PreviousData,但是,只有在ItemsSource
中没有任何项目时,这才有效空
If you know the maximum number of items in your collection, you can set the
AlternationCount
of yourListBox
to a number which is higher than the number of items in your collection, then use aDataTrigger
to determine the visibility or text of your separator items.Edit
As HB pointed out in his answer, you can also base your
DataTrigger
on RelativeSource.PreviousData, however that will only work if none of the items in yourItemsSource
arenull