奇怪的XAML错误:“" System.Windows.StaticResourceExtension"无法应用" [英] Odd XAML Error: '"System.Windows.StaticResourceExtension" cannot be applied'

查看:126
本文介绍了奇怪的XAML错误:“" System.Windows.StaticResourceExtension"无法应用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下XAML,以提供最新文档菜单,例如VS2012的文件">最新文档"菜单

I have the following XAML to provide a recent document menu like VS2012's FILE > Recent Documents menu

<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}}"/>
                                  <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都被突出显示,并且我收到了编译时错误(虽然代码可以运行并且有效!),说

This works! However, all my XAML for the FILE MenuItem block is being highlighted and I get a compile-time error (the code runs and works though!), saying

类型为"System.Windows.StaticResourceExtension"的对象不能应用于期望类型为"System.Windows.Style"的属性.

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

我正在使用.NET4.5和VS2012. 为什么会这样,我该如何解决?

I am using .NET4.5 and VS2012. Why is this happening and how can I resolve it?

感谢您的时间.

推荐答案

尝试类似的方法

您应该可以将Style移至任何ResourceDictionary,并且仍然可以使用,只需使用ItemContainerStyle

You should be able to move the Style to any ResourceDictionary and it should still work, you will just have to apply it to the Items inside the MenuItem using ItemContainerStyle

<Window.Resources>
    <Style x:Key="MyMenuStyle" TargetType="{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>
</Window.Resources>

<Grid>
    <Menu VerticalAlignment="Top">
        <MenuItem Header="_FILE" >
            <MenuItem Header="_Recent Studies" 
                      ItemsSource="{Binding RecentFiles}"
                      AlternationCount="{Binding RecentFiles.Count}"
                      ItemContainerStyle="{StaticResource MyMenuStyle}" />
            <Separator/>
            <MenuItem Header="E_xit" Height="22" Command="{Binding ExitCommand}" />
        </MenuItem>
    </Menu>

</Grid>

这篇关于奇怪的XAML错误:“&quot; System.Windows.StaticResourceExtension&quot;无法应用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆