如何在不受 ItemTemplate 影响的情况下向 ListBox 项添加分隔符

发布于 2024-12-27 19:55:07 字数 998 浏览 1 评论 0原文

下面您可以看到视图和视图模型。 结果将是:“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2025-01-03 19:55:07

您可以轻松地做到这一点:

  <TextBlock Margin="5">
      <Run Background="Red" Text="{Binding}"/>
      <Run Text="->"/>
  </TextBlock>

或者,如果您确实必须将其保留在数据模板之外,请使用 ItemContainerStyle 并将新的 Template 分配给 ListBoxItem,其中包含旁边的箭头ContentPresenter 项目模板所在的位置(这可能是一个好主意,因为这样您可以防止箭头显示为选中状态)。

编辑:我会使用带有PreviousData绑定的附加箭头来解决这个问题,如果它为空,则它之前没有项目:

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

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).

Edit: I would approach the issue with the additional arrow with a PreviousData binding, if it is null there is no item before it:

<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>
神仙妹妹 2025-01-03 19:55:07

如果您知道集合中的最大项目数,则可以将 ListBoxAlternationCount 设置为大于集合中项目数的数字,然后使用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>

编辑

正如HB在他的回答中指出的那样,您还可以将DataTrigger基于RelativeSource.PreviousData,但是,只有在 ItemsSource 中没有任何项目时,这才有效

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.

<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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文