XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.WinUIHelper

【DLL】WinUI的各种工具类,帮助开发者快速构建一个模块化的WinUI项目

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.UI.Xaml.Data;
using XFEExtension.NetCore.StringExtension;

namespace XFEExtension.NetCore.WinUIHelper.Utilities.Converter;

/// <summary>
/// Provides conversion logic for handling empty strings in data binding scenarios.
/// </summary>
/// <remarks>This converter transforms empty or null string values into a predefined placeholder string ("无")  and
/// vice versa. It is commonly used in UI applications to display a user-friendly representation  of empty
/// strings.</remarks>
public partial class EmptyStringConverter : IValueConverter
{
    /// <summary>
    /// Converts the specified value to a target type, returning a default representation if the value is null or empty.
    /// </summary>
    /// <param name="value">The value to be converted. If the value is null or an empty string, a default representation is returned.</param>
    /// <param name="targetType">The type to which the value is being converted. This parameter is not used in the current implementation.</param>
    /// <param name="parameter">An optional parameter that can be used to influence the conversion logic. This parameter is not used in the
    /// current implementation.</param>
    /// <param name="language">The language information for the conversion. This parameter is not used in the current implementation.</param>
    /// <returns>The original value if it is not null or empty; otherwise, the string "无".</returns>
    public object Convert(object value, Type targetType, object parameter, string language) => value.ToString().IsNullOrEmpty() ? "无" : value;

    /// <summary>
    /// Converts a value back to its original representation based on the specified logic.
    /// </summary>
    /// <param name="value">The value to be converted back. Typically a string representation.</param>
    /// <param name="targetType">The type to which the value is being converted. This parameter is not used in the current implementation.</param>
    /// <param name="parameter">An optional parameter for the conversion logic. This parameter is not used in the current implementation.</param>
    /// <param name="language">The language information for the conversion. This parameter is not used in the current implementation.</param>
    /// <returns>A string representing the converted value. Returns an empty string if the input value is "无"; otherwise, returns
    /// the original value.</returns>
    public object ConvertBack(object value, Type targetType, object parameter, string language) => value.ToString() == "无" ? string.Empty : value;
}