Macho Gwapito
Macho Gwapito

Reputation: 149

Add comma to string inside StringFormat in wpf

I have this textblock

   <TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat="{}{0}, {1}, {2}, ">
          <Binding Path="object.strProp1" />
          <Binding Path="object.strProp2" />
          <Binding Path="object.strProp3" />
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>

Let's assume object is not null and *strProp1* = "strProp1", *strProp2* = "strProp2", and *strProp2* = "strProp2".

The output for this would be something like this:

strProp1, strProp2, strProp3,

What I would like to know is how to remove the ',' whenever object is null or one of the properties is empty. That is, if object is null then the Textblock would just be empty. Or if one of the objects is empty then it will just be empty.

Any recommendations on how to this? Thanks! Edit: preferably in xaml only :)

Upvotes: 2

Views: 1819

Answers (3)

Abdullah Tahan
Abdullah Tahan

Reputation: 2129

<TextBlock TextWrapping="Wrap">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Address.FullAddress}" Value="">
                    <Setter Property="Visibility" Value="Hidden"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
    <Run Text="{Binding Address.LineOne}"/>
    <Run Text="{Binding 
        Address.LineTwo, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding 
        Address.City, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding Address.StateProvince}"/>
    <Run Text="{Binding 
        Address.Zip, 
        StringFormat='{}{0}, ',
        TargetNullValue=''}"/>
    <Run Text="{Binding Address.Country}"/>
</TextBlock>

Upvotes: 0

Martin Grundy
Martin Grundy

Reputation: 284

I know this is an old question but I was doing a similar thing and came up with this solution. You just need to escape the ',' as you would escape special characters in a string with a '\'.

So your binding would be:

<TextBlock>
   <TextBlock.Text>
     <MultiBinding StringFormat="{}{0}\, {1}\, {2}\, ">
       <Binding Path="object.strProp1" />
       <Binding Path="object.strProp2" />
       <Binding Path="object.strProp3" />
     </MultiBinding>
   </TextBlock.Text>
</TextBlock>

Upvotes: 5

Anand Murali
Anand Murali

Reputation: 4109

You have to use a Converter

MultiValueConverter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace DataBinding
{
    public class MultiStringConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null)
            {
                StringBuilder formattedString = new StringBuilder();
                int count = 0;
                foreach (var item in values)
                {
                    if (string.IsNullOrEmpty((String)item) == false)
                    {
                        if (count == 0)
                            formattedString.Append(item);
                        else
                            formattedString.Append(", " + item);
                        count++;
                    }

                }
                return formattedString.ToString();
            }
            else
                return null;

        }
        public object[] ConvertBack(object value, Type[] targetTypes,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("Cannot convert back");
        }
    }
}

XAML

<Window x:Class="DataBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataBinding"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MultiStringConverter x:Key="multiStringConverter"/>
    </Window.Resources>

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource multiStringConverter}">
                <Binding Path="object.strProp1" />
                <Binding Path="object.strProp2" />
                <Binding Path="object.strProp3" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

</Window>

Upvotes: 2

Related Questions