我有以下的XAML
<MenuItem Header="_Recent Studies"
Height="22"
ItemsSource="{Binding RecentFiles}">
<MenuItem.Resources>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Header" Value="{Binding FullFileName}"/>
</Style>
</MenuItem.Resources>
</MenuItem>它显示了我最近的文件

但是,我想像VS2012一样在文件名旁边显示VS2012的项目号。
为了做到这一点,我决定使用一个转换器,如果我只是得到没有文件名的数字,我可以做this。但是我想把它和多个绑定结合起来,这样我就可以写一些类似的东西
<MultiBinding StringFormat="{}{0}. {1}">
<Binding Path="??"/>
<Binding Path="FullFileName"/>
</MultiBinding>我不知道在上面写些什么。如何在我的文件名前加上文件在列表中的编号( FullFileName**s ),而不使用,在中添加索引属性,这会使事情变得更加复杂?**
耽误您时间,实在对不起。
编辑。这就是我如何在代码中使用下面的答案
<MenuItem Header="_FILE">
...
<MenuItem Header="_Recent Studies"
ItemsSource="{Binding RecentFiles}"
AlternationCount="{Binding RecentFiles.Count}"
HeaderTemplate="{x:Null}">
<MenuItem.Resources>
<Style TargetType="{x:Type MenuItem}"
BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}. {1}">
<Binding Path="(ItemsControl.AlternationIndex)"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}"
Converter="{StaticResource IntPlusNConverter}"/>
<Binding Path="FullFileName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</MenuItem.Resources>
</MenuItem>
<Separator/>
<MenuItem Header="E_xit"
Height="22"
Icon="{Binding Source={StaticResource Close},
Converter={StaticResource drawingBrushToImageConverter}}"
Command="{Binding ExitCommand}" />
</MenuItem>这行得通!但是,文件MenuItem块的所有XAML都被高亮显示,我得到一个编译时错误(代码运行并工作!)
类型"System.Windows.StaticResourceExtension“的对象不能应用于期望类型"System.Windows.Style”的属性。
为什么会发生这种情况,我如何解决?
耽误您时间,实在对不起。
结果!

发布于 2013-09-17 01:14:05
您应该能够像在这个答案中那样使用AlternationIndex来完成这个任务。
Finding Listbox Index of ListBoxItem (ItemsTemplate to specify Visual COntent)
您可能必须重写HeaderTemplate,因为StringFormat可能无法正常工作,因为Header是object而不是string。
示例:
<MenuItem Header="_Recent Studies" ItemsSource="{Binding RecentFiles}" AlternationCount="{Binding RecentFiles.Count}" HeaderTemplate="{x:Null}">
<MenuItem.Resources>
<Style TargetType="{x:Type MenuItem}" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="HeaderTemplate" >
<Setter.Value>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}. {1}">
<Binding Path="(ItemsControl.AlternationIndex)" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}" />
<Binding Path="FullFileName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</MenuItem.Resources>
</MenuItem>结果:

https://stackoverflow.com/questions/18839196
复制相似问题