I have a ListBox that looks like this:
<ListBox ItemsSource="{Binding Fruits}">
<!--Make the items wrap-->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name, StringFormat=' {0},'}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
This gives me a list like this:
Oranges, Grapes, Bananas,
But what I want is:
Oranges, Grapes, Bananas
(no trailing comma)
Any one have an idea on how to remove the trailing comma?
If you refactor your code, you can make a string that has your desired result very easily:
E.g. you could have:
(or, if the
Fruits
property was of a class you defined, you could override itsToString()
, which would be a good place to put theJoin
code)This can be achieved using IValueConverter to determine whether its
last item in a listBox and there by updating StringFormat on your binding using data trigger in XAML
.Create a converter to determine if value is last item in listbox -
Now modify your XAML and add trigger on DataTemplate to remove comma from StringFormat on your TextBlock -