wpfstring-formattingdynamicresource

WPF text StringFormat as DynamicResource


So i have this TextBlock:

<TextBlock
    Text="{Binding Path=Value, ElementName=progressBarColumn, StringFormat={}{0:N2}%}"
    VerticalAlignment="Center"
    HorizontalAlignment="Center"
    Foreground="{DynamicResource ProgressBarForegroundColor}"
    FontFamily="{DynamicResource ProgressBarFontFamily}"
    FontSize="{DynamicResource ProgressBarFontSize}"/>

And i want to be able to control this String Format from N2 to N1 etc. so i created this :

<system:String x:Key="ProgressBarStringFormat">N2</system:String>

Usage:

Text="{Binding Path=Value, ElementName=progressBarColumn, StringFormat={}{0:ProgressBarStringFormat}%}"

And in my Progress-Bar instead of seen the Value i only see ProgressBarStringFormat text.


Solution

  • You can't have anything but a literal for a property of a Binding. But if you're willing to use a ContentControl or a Label instead of a TextBlock, you can stick a DynamicResource or a Binding on the ContentStringFormat property:

    <Label
        Content="{Binding Value, ElementName=progressBarColumn}"
        ContentStringFormat="{DynamicResource ProgressBarStringFormat}"
        VerticalAlignment="Center"
        HorizontalAlignment="Center"
        Foreground="{DynamicResource ProgressBarForegroundColor}"
        TextElement.FontFamily="{DynamicResource ProgressBarFontFamily}"
        TextElement.FontSize="{DynamicResource ProgressBarFontSize}"
        />