WPF Binding - Default value for empty string

后端 未结 4 572
走了就别回头了
走了就别回头了 2020-11-28 11:19

Is there a standard way to set a default or fallback value for a WPF binding if the bound string is empty?



        
4条回答
  •  悲&欢浪女
    2020-11-28 11:46

    You could use a converter and do the respective validation on it.

    Binding="{Binding Path=Name, Converter={StaticResource nameToOtherNameConverter}}"
    

    and in your converter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!string.IsNullOrEmpty(value.ToString()))
            { /*do something and return your new value*/ }
    

提交回复
热议问题