2017-09-29 3 views
0

j'ai donc ce TextBlock:WPF texte StringFormat comme DynamicResource

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

Et je veux être en mesure de contrôler ce String FormatN2-N1 etc donc je créé ceci:

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

Utilisation :

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

Et dans ma Progress-Bar au lieu de vu le Value je ne vois que ProgressBarStringFormat texte.

Répondre

0

Vous ne pouvez rien avoir d'autre qu'un littéral pour une propriété d'une liaison. Mais si vous êtes prêt à utiliser un ContentControl ou une étiquette au lieu d'un TextBlock, vous pouvez coller un DynamicResource ou une liaison sur sa propriété ContentStringFormat:

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

Je suis la mise en marge à zéro parce que l'étiquette aura une marge par défaut définie dans son style implicite, contrairement à TextBlock.