我有一个StatusBarItem,我想在任何给定时刻显示以下消息"X/Y“,其中X是当前所选元素的行数,Y是行数。
现在,如果我在xaml中使用Content="{Binding ElementName=lvTabela, Path=SelectedIndex}"代码,我可以获得要显示的第一个属性,但我不确定如何才能同时获得这两个属性。
我想我总是可以同时使用两个StatusBarItem元素,但我也想学习如何做到这一点。
哦,既然我们这样做了,我该如何递增选定的索引呢?基本上,我希望它显示0到rowCount,而不是-1到rowCount-1。我见过人们使用格式化程序向他们的数据绑定添加额外的文本,但我不确定如何才能像这样操作数据。
发布于 2013-05-08 04:20:59
你有两个选择:
将StatusbarItem的Content设置为将StringFormat与MultiBinding一起使用,如下所示:
<StatusBarItem>
<StatusBarItem.Content>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}/{1}">
<MultiBinding.Bindings>
<Binding ElementName="listView"
Path="SelectedIndex" />
<Binding ElementName="listView"
Path="Items.Count" />
</MultiBinding.Bindings>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StatusBarItem.Content>
</StatusBarItem>或者在MultiBinding上使用转换器,而不必使用TextBlock
<Window.Resources>
<local:InfoConverter x:Key="InfoConverter" />
</Window.Resources>
...
<StatusBarItem>
<StatusBarItem.Content>
<MultiBinding Converter="{StaticResource InfoConverter}">
<MultiBinding.Bindings>
<Binding ElementName="listView"
Path="SelectedIndex" />
<Binding ElementName="listView"
Path="Items.Count" />
</MultiBinding.Bindings>
</MultiBinding>
</StatusBarItem.Content>
</StatusBarItem>和InfoConverter.cs:
class InfoConverter : IMultiValueConverter {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
return values[0].ToString() + "/" + values[1].ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
throw new NotImplementedException();
}
}当StringFormat返回一个字符串时,StatusBarItem需要一个对象,因此我们不能在没有TextBlock的情况下将StringFormat与MultiBinding一起使用,因为TextBlock可以在它的Text字段中接受一个字符串。
至于你的第二个关于如何增加SelectedIndex值的问题,你可以很容易地用Converter来做,
只需将InfoConverter.cs中的Convert(...)函数切换为
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
return (System.Convert.ToInt32(values[0]) + 1).ToString() + "/" + values[1].ToString();
}https://stackoverflow.com/questions/16427210
复制相似问题