How to replace strings in StringFormat in WPF Binding

后端 未结 1 1491
南笙
南笙 2021-01-24 09:14

I need to replace a , with ,\\n(New Line) in my string i want to do it on ClientSide in StringFormat



        
1条回答
  •  梦毁少年i
    2021-01-24 09:39

    You can't do this via a StringFormat binding operation, as that doesn't support replacement, only composition of inputs.

    You really have two options - expose a new property on your VM that has the replaced value, and bind to that, or use an IValueConverter to handle the replacement.

    A value converter could look like:

    public class AddNewlineConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string original = Convert.ToString(value);
            return original.Replace(",", ",\n");
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplemnentedException();
        }
    }
    

    You'd then use this in your binding. You could add a resource:

    
        
    
    

    In your binding, you'd change this to:

    
    

    0 讨论(0)
提交回复
热议问题