2

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

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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>

3 Comments

Thanks for the answer! I'll think about this... I just have another question, is there no other way to do this with just xaml?
I did a quick search msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx and a pure XAML solution isn't possible.
you can short this converter to be return string.Join(", ", values.Where(o => !string.IsNullOrEmpty(o as string)));
0
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.