Xaml StringFormat和静态字符串

Chr*_*ray 10 wpf xaml

我想看看是否有一种结合日期时间字符串格式和静态字符串的方法.

所以目前我可以用这样的文字格式化我的日期和前缀:

<TextBlock Text="{Binding MyDate StringFormat=Started {0:dd-MMM-yyyy HH:mm}}"
Run Code Online (Sandbox Code Playgroud)

结果如下:

Started 01-Jan-2011 12:00
Run Code Online (Sandbox Code Playgroud)

在过去,我已经能够使用静态字符串来保持日期的通用格式; 像这样(注意没有前缀文本):

<TextBlock Text="{Binding MyDate, StringFormat={x:Static i:Format.DateTime}}" />
Run Code Online (Sandbox Code Playgroud)

i:Format带静态属性的静态类在哪里DateTime返回字符串"dd-MMM-yyyy HH:mm"

所以我要问的是什么; 有没有办法组合这些方法,以便我可以前缀我的日期并使用常见的静态字符串格式化程序?

Cod*_*ked 2

您可以使用类似的东西来代替绑定:

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;
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它{local:DateTimeFormattedBinding MyDate, CustomStringFormat=Started %date%}

您也可以使替换通用,并通过不同的属性(或多个属性)进行设置。