Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xaml StringFormat and static strings

Tags:

wpf

xaml

I want to see if theres a way to combine datetime string format and static strings.

So currently I can format my date and prefix with text like this:

<TextBlock Text="{Binding MyDate StringFormat=Started {0:dd-MMM-yyyy HH:mm}}"

Results in this:

Started 01-Jan-2011 12:00

In the past I've been able to use a static string to keep a common format for my dates; like this (Note no prefixed text):

<TextBlock Text="{Binding MyDate, StringFormat={x:Static i:Format.DateTime}}" />

Where i:Format is a static class with a static property DateTime that returns the string "dd-MMM-yyyy HH:mm"

So what I'm asking; is there a way to combine these methods so that I can prefix my date and use the common static string formatter?

like image 805
Chris Moutray Avatar asked Apr 08 '11 10:04

Chris Moutray


People also ask

What is the stringformat property in JavaScript?

The StringFormat property only makes sense when the target property is of type string, and the binding mode is OneWay or TwoWay. For two-way bindings, the StringFormat is only applicable for values passing from the source to the target. As you'll see in the next article on the Binding Path, data bindings can become quite complex and convoluted.

How to format a string without curly braces in XAML?

Notice that the formatting string is delimited by single-quote (apostrophe) characters to help the XAML parser avoid treating the curly braces as another XAML markup extension. Otherwise, that string without the single-quote character is the same string you'd use to display a floating-point value in a call to String.Format.

What is a formatting string?

The formatting string includes formatting codes specific to various types of objects, and you can include other text along with the values being formatted. See the Formatting Types in .NET article for more information on string formatting.

What does the stringformat attribute do?

Well Okay: As you can see the StringFormat attribute can be a time saver, and just make life a little easier. One thing to note is that if you use the StringFormat attribute and you bind to a property that has no value, otherwise known as null, then the text that will be displayed is “ {DependencyProperty.UnsetValue}".


1 Answers

You could use something like this in place of the Binding:

public class DateTimeFormattedBinding : Binding {
    private string customStringFormat = "%date%";

    public DateTimeFormattedBinding () {
        this.StringFormat = Format.DateTime;
    }

    public DateTimeFormattedBinding (string path)
        : base(path) {
        this.StringFormat = Format.DateTime;
    }

    public string CustomStringFormat {
        get {
            return this.customStringFormat;
        }
        set {
            if (this.customStringFormat != value) {
                this.customStringFormat = value;
                if (!string.IsNullOrEmpty(this.customStringFormat)) {
                    this.StringFormat = this.customStringFormat.Replace("%date%", Format.DateTime);
                }
                else {
                    this.StringFormat = string.Empty;
                }
            }
        }
    }
}

Then use it like {local:DateTimeFormattedBinding MyDate, CustomStringFormat=Started %date%}

You could probably make the replacement generic also, and set it via a different property (or properties).

like image 125
CodeNaked Avatar answered Sep 21 '22 14:09

CodeNaked