locked
Databinding and Trimming RRS feed

  • Question

  • I have a field that I'm binding to a TextBlock in a ListBox.  The field in database is a fixed 250 chars long.  How do I trim the ending spaces in Blend?  Thanks
    Thursday, April 2, 2009 12:31 PM

Answers

  • You have to create a Wrapper-Class which inherits from IValueConverter.
    public class ListBoxConverter:IValueConverter
        {
    
            #region IValueConverter Members
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if (value == DBNull.Value || (string)value == string.Empty)
                {
                    return string.Empty;
                }
                string strInput = ((string)value).Trim();
                return strInput;     
       }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    In the page you have to load this Class in the Resourcesection
     <UserControl.Resources>       
            <local:LisBoxConverterx:Key="LisBoxConverterx" />
        </UserControl.Resources>
    and finally the Binding looks something like this:
    .Text = "{Binding = Control.Text, Converter ={StaticResource ListBoxConverter}}" />


    • Marked as answer by shaggygi Monday, April 6, 2009 3:03 PM
    Monday, April 6, 2009 2:50 PM