Windows Phone 8.1 XAML StringFormat

后端 未结 3 1984
迷失自我
迷失自我 2020-12-15 07:27

I am trying to display some text along with binded data, for example, I have the code:



        
相关标签:
3条回答
  • 2020-12-15 08:08

    Like @KooKiz pointed out StringFormat at the moment isn't supported, but you could accomplish the same effect just breaking out your lines into inline Runs without a converter like;

    <TextBlock>
       <Run Text="Hey I wanted to put this text in front of "/>
       <Run Text="{Binding Path=Shorthand}"/>
       <Run Text=" and I also wanted some text after it. Neato.."/>
    </TextBlock>
    

    Hope this helps, cheers.

    0 讨论(0)
  • 2020-12-15 08:08

    I used this approach (Written by Microsoft): https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.data.ivalueconverter

    It works great!

    0 讨论(0)
  • 2020-12-15 08:10

    StringFormat isn't supported on WinRT. However, you can easily replace it by creating a custom converter:

    public class StringFormatConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            return string.Format(parameter as string, value);
        }  
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return null;
        }
    }
    

    Then declare it in your page resources:

    <Page.Resources>
        <local:StringFormatConverter x:Name="StringFormat"/>
    </Page.Resources>
    

    And use it in your bindings:

    <TextBlock Text="{Binding Path=SomeText, Converter={StaticResource ResourceKey=StringFormat}, ConverterParameter='Hello {0}'}" />
    
    0 讨论(0)
提交回复
热议问题