【问题标题】:MultiBinding using DisplayMemeberBinding for MenuItem对 MenuItem 使用 DisplayMemeberBinding 进行多重绑定
【发布时间】:2013-09-17 18:48:10
【问题描述】:

我有以下 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 一样在文件名旁边显示 MenuItem 的项目编号。

  1. 文件名A.f
  2. 文件名B.x
  3. 文件名C.j

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

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

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

感谢您的宝贵时间。


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

<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 块的所有 XAML 都被突出显示,并且我得到一个编译时错误(代码运行并且可以工作!),说

“System.Windows.StaticResourceExtension”类型的对象不能应用于需要“System.Windows.Style”类型的属性。

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

感谢您的宝贵时间。


结果!

【问题讨论】:

    标签: c# wpf binding menuitem


    【解决方案1】:

    您应该可以像在这个答案中那样使用AlternationIndex 来做到这一点

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

    您可能必须覆盖HeaderTemplate,因为StringFormat 可能无法正常工作,因为Headerobject 而不是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>
    

    结果:

    【讨论】:

    • 伙计,你真的了解 WPF!我的目标是变得无所不能,就像你'大师'......
    • 嗨,伙计,这个解决方案显然可以工作,但是当我将样式包含在我拥有的位置的代码中时,我得到了an object of the type "system.windows.staticresourceextension" cannot be applied to a property that expects the type "system.windows.style".。我已将样式移至ResourceDictionary 并尝试提供Stylex:Key="..." 并在MainWindow 中使用它,但这不起作用你能建议吗?
    • 你能告诉我你是如何在主窗口中使用这个样式的吗,它似乎可以在我移动这个样式的任何地方找到
    • 我复制了你的代码,它似乎工作正常(除了我没有的转换器),你用的是什么VisualStudio版本,我可以试试
    • VS2012。不知道为什么它会出现这个错误,因为代码工作正常并且准确地显示了我想要的。也许我会问另一个关于这个的问题......有没有一种简单的方法可以在不破坏AlternatingCount 的情况下将样式移动到ResourceDictionary
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2018-03-31
    • 2011-10-04
    • 2015-01-20
    • 2011-07-15
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多