-1

I have a name such as Jonny Bravo, and I would like my label to reflect the initials of that name (JB) through Binding. How can I?

I need a code entirely through XAML/Binding and possible ValueConverter if needed. Any suggestions?

1
  • put a property in your ViewModel, as a quick solution, otherwise implement a converter. What have you tried?
    – Fede
    Mar 3, 2015 at 20:40

3 Answers 3

1

Use a ValueConverter.

Converter:

public class InitialsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = value as string;
        string i = string.Empty;

        if (s != null)
        {
            string[] split = s.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            foreach (string piece in split)
            {
                i += piece[0];
            }
        }

        return i;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Xaml use:

<TextBox Text="{Binding Name, Converter={StaticResource InitialsConverter}}" />
11
  • I have a one property for the Name and one for the Initials. And I want to Initials to update automatically based on the Initials of the Name field. Can you post some examples? Mar 3, 2015 at 20:47
  • It would probably easier to just have a converter for where you want to display the initials, that way you wouldn't need to have another property. Mar 3, 2015 at 20:51
  • yes, i was thinking that. do you have any code examples to that effect? Mar 3, 2015 at 20:51
  • i edited the answer to show one possible way for the converter to work. you may want to think about some additional logic, because names with ie. III or , Jr. will not work correctly though. It will work for First, Middle, Last etc. though as is Mar 3, 2015 at 21:02
  • your code works, but only brings the first name for some reason. any ideas why? because in your code i can clearly see it splittling words. Mar 3, 2015 at 21:19
0

Fill the Initials Property inside the Setter of the FullName Property at your viewModel

Public string FullName{
...
Set{
this.fullName = value;
this.Initials = GenerateInitialsFromFullName();
}

Or create an ValueConverter as already suggested.

0

Using a value converter would be the way to go because it's reusable in other places in your code if needed.

Here's a quick one I threw together using Regex to find the first letters (note that splitting the string would offer better performance).

public class InitialsConverter : IValueConverter
{
    public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string str = value as string;
        if (str != null)
        {
            string s = "";
            MatchCollection matches = Regex.Matches(str, @"(\b\w)");
            foreach (Match m in matches)
                s += m.Value;
            return s;
        }
        else
        {
            return null;
        }
    }

    public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Usage:

<!--Declare in your scope's resources-->
<Window.Resources>
    <r:InitialsConverter x:Key="initials"/>
</Window.Resources>

<!--Bind to a string using the converter-->
<TextBlock Text="{Binding MyName, Converter={StaticResource initials}}"/>

No converter:

Using converter:

2
  • both work, but I only get the first letter for some reason. I am using a label, and point to that label within the same window. any ideas? Mar 3, 2015 at 21:17
  • Chuck a break point in the InitialsConverter.Convert method and step through -- you may be able to work out what's wrong this way.
    – learningcs
    Mar 3, 2015 at 21:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.