Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:20:04+00:00 2026-05-23T14:20:04+00:00

I have an ItemsControl which lists items by separating them with a comma. The

  • 0

I have an ItemsControl which lists items by separating them with a comma. The code is the following:

<ItemsControl ItemsSource="{Binding MyItems}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text=", " 
                           Name="commaTextBlock"/>
                <TextBlock Text="{Binding}"/>
            </StackPanel>
            <!-- Hide the first comma -->
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding 
                        RelativeSource={RelativeSource PreviousData}}" 
                             Value="{x:Null}">
                    <Setter Property="Visibility" 
                            TargetName="commaTextBlock" 
                            Value="Collapsed"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The result is something like this: Item1, Item2, Item3

Now, I’d like to do the same using a WrapPanel instead of a StackPanel as the ItemsPanelTemplate. I tested it and it works fine, except for a small detail, it does something like this:

Item1, Item2

, Item3

Of course this is because the comma is before each element and I hide the first one. I would like to put the comma after each element and hide the last one, so the result would be this:

Item1, Item2,

Item3

It would be really simple if there existed something like NextData (so I would bind to this instead of to PreviousData), but unfortunately no such thing exists (or I haven’t found one). Does anyone have an idea how to solve this problem?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:20:04+00:00Added an answer on May 23, 2026 at 2:20 pm

    Maybe you can try to use multibinding and a converter. Something like this:

    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding Converter="{StaticResource myConverter}">
              <Binding Mode="OneWay"/>
              <Binding ElementName="root" Path="ItemsSource" Mode="OneWay"/>
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
    

    Where root is the name of your ItemsControl.

    And write a converter which checks for position:

    public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var text = values[0] as string;
            var list = values[1] as ObservableCollection<string>;
    
            //check for null etc...
    
            if (list.IndexOf(text) == list.Count - 1)
                return text;
    
            return string.Format("{0}, ", text);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
           //ignore this, not gonna happen with OneWay Binding 
           return null;
        }
    }
    

    Works for me! Hope it helps you with your problem!

    EDIT:
    Almost the same as the other answer, the difference here is that you only need 1 TextBlock in your template, and the converter decides if there is a comma or not. But basically the same principle. MultiBinding rocks! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ItemsControl with the following ItemTemplate: <DataTemplate x:Key=myItemTemplate> <TextBlock Height=??? Text={Binding Path=Description}
I have a custom user control in XAML that creates a ItemsControl which ItemsPanel
I want to have an ItemsControl in which the items are displayed horizontally .
I have an ItemsControl which uses a Grid as the ItemsPanelTemplate , and sets
I have an ItemsControl whose ItemsSource gets bound to an ObservableCollection<Component> at run time.
I have a WPF ItemsControl who's ItemsSource is bound to an observable collection of
I'm trying to display a list of Notes. I have an ItemsControl which is
I have an ItemsControl which is giving me issues. It has a DataTemplate, which
I have created a custom user control, which is basicly a ItemsControl with it's
I'm doing a list of contacts for a person. I have an ItemsControl which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.