c# - 为 MenuItem 使用 DisplayMemeberBinding 的多重绑定(bind)

标签 c# wpf binding menuitem

我有以下 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>

其中显示了我最近的文件,如

Recents

但是,我想像 VS2012 那样在文件名旁边显示 MenuItem 的项目编号。

  1. FileNameA.f
  2. FileNameB.x
  3. FileNameC.j

等为此,我决定使用一个转换器,如果我只是获取没有文件名的数字,我可以做 this .但我想将它与多重绑定(bind)结合起来,这样我就可以写出类似的东西

<MultiBinding StringFormat="{}{0}. {1}">
    <Binding Path="??"/> 
    <Binding Path="FullFileName"/>
</MultiBinding>

上面不知道写什么。 如何在不向我的 FullFileName 添加索引属性的情况下将文件在列表中的编号作为文件名的前缀,这会使事情变得更复杂?

感谢您的宝贵时间。


编辑。这就是我在代码中使用以下答案的方式

<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>

这行得通!但是,我的 FILE MenuItem block 的所有 XAML 都被突出显示,我得到一个编译时错误(代码运行并且有效!),说

An object of the type "System.Windows.StaticResourceExtension" cannot be applied to a property that expects the type "System.Windows.Style".

为什么会发生这种情况,我该如何解决?

感谢您的宝贵时间。


结果!

enter image description here

最佳答案

你应该能够像这个答案中那样使用 AlternationIndex 来做到这一点

Finding Listbox Index of ListBoxItem (ItemsTemplate to specify Visual COntent)

您可能必须覆盖 HeaderTemplate,因为 StringFormat 可能无法正常工作,因为 Header 是一个 object不是字符串

例子:

<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>

结果:

enter image description here

关于c# - 为 MenuItem 使用 DisplayMemeberBinding 的多重绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839196/

相关文章:

c# - LINQ 中的翻转分组

c# - 更改 Windows 窗体应用程序中的字体大小

c# - AnyCPU 与 x64 平台在 64 位机器上的性能差异

.net - 重新排序 WPF TabControl 中的选项卡

wpf - 资源字典没有延迟加载

wpf - 如何停止更新绑定(bind)属性?

c# - 链接到图表或调度控件的示例

c# - WPF: MemoryStream 占用大量内存

C# DataGrid 填充了多层列表

objective-c - 如何将 UIButton 属性绑定(bind)到另一个属性?