Back to Devexpress

Badge.ContentStringFormat Property

wpf-devexpress-dot-xpf-dot-core-dot-badge-334048e5.md

latest4.4 KB
Original Source

Badge.ContentStringFormat Property

Gets or sets a composite string that specifies how to format the Content property if it is displayed as a string. This is a dependency property.

Namespace : DevExpress.Xpf.Core

Assembly : DevExpress.Xpf.Core.v25.2.dll

NuGet Package : DevExpress.Wpf.Core

Declaration

csharp
public string ContentStringFormat { get; set; }
vb
Public Property ContentStringFormat As String

Property Value

TypeDescription
String

A composite String that specifies how to format the Badge.Content property if it is displayed as a string.

|

Remarks

The following code sample specifies the ContentStringFormat and uses the Badge.ContentFormatProvider property to specify the Badge ‘s content format:

xaml
<dx:SimpleButton
   HorizontalAlignment="Center"
   VerticalAlignment="Center"
   Glyph="{dx:SvgImageSource Uri=Glyph_Message.svg}">
   <dx:Badge.Badge>
       <dx:Badge Content="150" BadgeKind="Error"
                 ContentStringFormat="{}{0:length2}"
                 ContentFormatProvider="{local:BadgeFormatProvider}"/>
   </dx:Badge.Badge>
</dx:SimpleButton>
csharp
public class BadgeFormatProvider : MarkupExtension, IFormatProvider, ICustomFormatter {
   static readonly Regex formatExpression = new Regex(@"length(\d+)", RegexOptions.Compiled);

   public object GetFormat(Type formatType) { return this; }
   public override object ProvideValue(IServiceProvider serviceProvider) { return this; }
   public string Format(string format, object arg, IFormatProvider formatProvider) {
       if(arg==null)
           return String.Empty;

       var match = formatExpression.Match(format);
       if(!match.Success)
           return String.Format(CultureInfo.CurrentCulture, format ?? "{0}", arg);

       var length = int.Parse(match.Groups[1].Value);
       var inputString = Convert.ToString(arg);
       if (inputString.Length <= length)
           return inputString;

       if (decimal.TryParse(inputString, out _))
           return (Math.Pow(10, length) - 1) + "+";

       return inputString.Substring(0, length) + "...";
   }
}
vb
Public Class BadgeFormatProvider
    Inherits MarkupExtension
    Implements IFormatProvider, ICustomFormatter

    Shared ReadOnly formatExpression As Regex = New Regex("length(\d+)", RegexOptions.Compiled)

    Public Function GetFormat(ByVal formatType As Type) As Object
        Return Me
    End Function

    Public Overrides Function ProvideValue(ByVal serviceProvider As IServiceProvider) As Object
        Return Me
    End Function

    Public Function Format(ByVal format As String, ByVal arg As Object, ByVal formatProvider As IFormatProvider) As String
        If arg Is Nothing Then Return String.Empty
        Dim match = formatExpression.Match(format)
        If Not match.Success Then Return String.Format(CultureInfo.CurrentCulture, If(format, "{0}"), arg)
        Dim length = Integer.Parse(match.Groups(1).Value)
        Dim inputString = Convert.ToString(arg)
        If inputString.Length <= length Then Return inputString
        If Decimal.TryParse(inputString, __) Then Return (Math.Pow(10, length) - 1) & "+"
        Return inputString.Substring(0, length) & "..."
    End Function
End Class

Tip

Refer to the Formatting types in .NET topic for more information.

See Also

Badge.ContentFormatProvider Property

Badge Class

Badge Members

DevExpress.Xpf.Core Namespace