[英]How to add delimiter to ListBox Items without being effected by the ItemTemplate
Below you can see the View and the ViewModel. 在下面,您可以看到View和ViewModel。 The result will be :" ABC " that the background of each letter is red.
结果将是:“ ABC”,每个字母的背景为红色。 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 .
那意味着应该是这样的:“ A-> B-> C”,只有字母会用红色上色,而箭头则不会 。 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: 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; }
}
You could easily do this: 您可以轻松地做到这一点:
<TextBlock Margin="5">
<Run Background="Red" Text="{Binding}"/>
<Run Text="->"/>
</TextBlock>
Or if you really must keep it out of the data template use the ItemContainerStyle
and assign a new Template
to the ListBoxItem
which contains an arrow next to the ContentPresenter
where the item template will be (this might be a good idea as you then can prevent the arrow from appeearing selected). 或者,如果您确实必须将其保留在数据模板之外,请使用
ItemContainerStyle
并将新Template
分配给ListBoxItem
,该模板在将要放置项目模板的ContentPresenter
旁边包含一个箭头(这可能是个好主意,因为这样可以防止从出现的箭头中选择)。
Edit: I would approach the issue with the additional arrow with a PreviousData
binding, if it is null there is no item before it: 编辑:我将使用带有
PreviousData
绑定的附加箭头来解决此问题,如果为null,则之前没有任何项:
<DataTemplate>
<!-- StackPanel because Runs can't be collapsed, you could clear their text though -->
<StackPanel Orientation="Horizontal">
<TextBlock Text="->">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource PreviousData}}"
Value="{x:Null}">
<Setter Property="Visibility" Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="{Binding}" Background="Red" />
</StackPanel>
</DataTemplate>
If you know the maximum number of items in your collection, you can set the AlternationCount
of your ListBox
to a number which is higher than the number of items in your collection, then use a DataTrigger
to determine the visibility or text of your separator items. 如果知道集合中的最大项目数,则可以将
ListBox
的AlternationCount
设置为一个大于集合中项目数的数字,然后使用DataTrigger
确定分隔项的可见性或文本。
<Style x:Key="ArrowTextBlockStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Text" Value="->" />
<Style.Triggers>
<DataTrigger Value="0" Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=(ItemsControl.AlternationIndex)}">
<Setter Property="Text" Value="" />
</DataTrigger>
</Style.Triggers>
</Style>
<ListBox AlternationCount="100" ItemsSource="{Binding MyArray}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5">
<TextBlock Style="{StaticResource ArrowTextBlockStyle}" />
<TextBlock Text="{Binding }" Background="Red" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
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 your ItemsSource
are null
就像HB在他的回答中指出的那样,您也可以将
DataTrigger
基于RelativeSource.PreviousData ,但是仅当ItemsSource
中的所有项目都不为null
, DataTrigger
起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.