VPet/VPet.Solution/Converters/StringFormatConverter.cs

50 lines
1.3 KiB
C#
Raw Normal View History

2023-12-30 15:50:37 +00:00
using System.Globalization;
2023-12-18 14:53:56 +00:00
2023-12-30 15:50:37 +00:00
namespace HKW.WPF.Converters;
2023-12-18 14:53:56 +00:00
/// <summary>
2023-12-30 15:50:37 +00:00
/// 字符串格式化器
2023-12-18 14:53:56 +00:00
/// <para>示例:
/// <code><![CDATA[
/// <MultiBinding Converter="{StaticResource MarginConverter}">
/// <Binding Path="StringFormat" />
/// <Binding Path="Value1" />
/// <Binding Path="Value2" />
/// </MultiBinding>
/// OR
/// <MultiBinding Converter="{StaticResource MarginConverter}" ConverterParameter="{}{0}{1}">
/// <Binding Path="Value1" />
/// <Binding Path="Value2" />
/// </MultiBinding>
/// ]]></code></para>
/// </summary>
2023-12-30 15:50:37 +00:00
public class StringFormatConverter : MultiValueConverterBase
2023-12-18 14:53:56 +00:00
{
2023-12-30 15:50:37 +00:00
/// <summary>
///
/// </summary>
/// <param name="values"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public override object Convert(
2023-12-18 14:53:56 +00:00
object[] values,
Type targetType,
object parameter,
2023-12-30 15:50:37 +00:00
CultureInfo culture
2023-12-18 14:53:56 +00:00
)
{
var formatStr = (string)parameter;
if (string.IsNullOrWhiteSpace(formatStr))
{
formatStr = (string)values[0];
return string.Format(formatStr, values.Skip(1).ToArray());
}
else
{
return string.Format(formatStr, values);
}
}
}