bla*_*ord 15 c# xaml windows-phone-8.1
我试图显示一些文本以及绑定数据,例如,我有代码:
<TextBlock Text="{Binding Shorthand}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
Run Code Online (Sandbox Code Playgroud)
我想在'速记'之前添加一些文本,从我读过的内容可以通过使用StringFormat作为Binding的属性来实现,这有点类似于:
<TextBlock Text="{Binding Path=Shorthand, StringFormat={0} text I want to add}" Style="{ThemeResource ListViewItemTextBlockStyle}" />
Run Code Online (Sandbox Code Playgroud)
然而,这似乎不起作用,这是不是在8.1中做事的方式?
Kev*_*sse 30
StringFormatWinRT不支持.但是,您可以通过创建自定义转换器轻松替换它:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在页面资源中声明它:
<Page.Resources>
<local:StringFormatConverter x:Name="StringFormat"/>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
并在绑定中使用它:
<TextBlock Text="{Binding Path=SomeText, Converter={StaticResource ResourceKey=StringFormat}, ConverterParameter='Hello {0}'}" />
Run Code Online (Sandbox Code Playgroud)
Chr*_* W. 16
就像@KooKiz目前所指出的那样StringFormat不受支持,但你可以完成相同的效果,只需将你的线条分解为内联运行而不需要像转换器一样;
<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>
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,欢呼.
| 归档时间: |
|
| 查看次数: |
7914 次 |
| 最近记录: |