我有一个标签,在这里我要显示列表中所选项目的数量(列表项是从网格中选择的)。这一切都运行良好,标签显示数字。我想要的是使标签显示"5项选定“。现在,我只知道数字5。这是xaml:
<Label Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<Label.Content>
<Binding Path="SelectedRows.Count" />
</Label.Content>
</Label>我已经接近这家伙了。
发布于 2014-07-24 13:46:10
只需在绑定中指定StringFormat即可。
这应该可以
<Binding Path="SelectedRows.Count" StringFormat="{}{0} items selected"/>以上可能不适用于标签,因为它遵循内容模型,因此您可能需要使用TextBlock代替。
示例
<TextBlock Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<TextBlock.Text>
<Binding Path="SelectedRows.Count"
StringFormat="{}{0} items selected"/>
</TextBlock.Text>
</TextBlock>或
<TextBlock Height="23"
HorizontalAlignment="Left"
Margin="7,2,0,0"
Name="lblSelectionSummary"
VerticalAlignment="Top"
Width="557"
FontFamily="Arial"
Text="{Binding SelectedRows.Count, StringFormat={}{0} items selected}" />在标签中使用字符串格式
绑定中的StringFormat适用于字符串类型属性,因为标签的内容属性类型是对象,所以StringFormat不能工作。
感谢盲人的暗示
由于标签遵循内容模型,它使用ContentStringFormat对值进行格式化,下面是一个使用相同的示例
<Label Content="{Binding SelectedRows.Count}"
ContentStringFormat="{}{0} items selected" />https://stackoverflow.com/questions/24935448
复制相似问题