开发者

ContextMenu in Style setter

开发者 https://www.devze.com 2023-01-07 17:02 出处:网络
Does anyone know why this would produce an XamlParseException \"Cannot add content of type \'System.Windows.Controls.ContextMenu\' to an object of type \'System.Object\'\":

Does anyone know why this would produce an XamlParseException "Cannot add content of type 'System.Windows.Controls.ContextMenu' to an object of type 'System.Object'":

<ItemsControl>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas ClipToBounds="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContextMenu">
                <Setter.Value>
                    <ContextMenu>
                        <MenuItem Header="Remove" />
                    </ContextMenu>
                </Setter.Value>
            </Setter>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

And the following works just fine?

<ItemsControl>
    <ItemsControl.Resources>
        <ContextMenu x:Key="NodeContextMenu">
            <MenuItem Header="Remove" />
        </ContextMenu>
    </ItemsControl.Resources>
  开发者_运维百科  <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas ClipToBounds="True"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="ContextMenu" Value="{StaticResource NodeContextMenu}" />
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>


Looks like a bug that has since been fixed - the code in question generates a XAML parse exception when targeting .NET 3.5, but works fine for me when targeting .NET 4.0. If you follow the link provided in the comments above, you will find a connect issue with comments from others indicating the problem has gone away with .NET 4.0.

0

精彩评论

暂无评论...
验证码 换一张
取 消