Why does an x:Array element cause any x:Shared attribute following it to be ignored?

前端 未结 1 1278
[愿得一人]
[愿得一人] 2021-01-19 08:34

I suspect a XAML compiler and/or WPF bug, but I want to make sure I haven\'t done something wrong here (other than trusting the XAML compiler and/or WPF, that is :) ).

相关标签:
1条回答
  • 2021-01-19 08:45

    Write <ResourceDictionary> explicitely into <Window.Resources>:

    <Window x:Class="TestxSharedMenuItem.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:s="clr-namespace:System;assembly=mscorlib"
            Title="MainWindow" Height="350" Width="525">
      <Window.Resources>
        <ResourceDictionary> <!-- modified here -->
          <x:Array x:Key="menuItemValues1" Type="{x:Type s:String}">
            <s:String>value #1</s:String>
            <s:String>value #2</s:String>
            <s:String>value #3</s:String>
          </x:Array>
          <MenuItem x:Key="menuItem1" x:Shared="False"
                    ItemsSource="{StaticResource menuItemValues1}"
                    Header="Shared menu item"/>
        </ResourceDictionary> <!-- modified here -->
      </Window.Resources>
      <StackPanel>
        <Menu HorizontalAlignment="Left" VerticalAlignment="Top">
          <StaticResource ResourceKey="menuItem1"/>
          <StaticResource ResourceKey="menuItem1"/>
        </Menu>
      </StackPanel>
    </Window>
    

    I had a very similar issue (actually, quite exactly the same, only with UserControl). I was desperate when I tried the above workaround :), but it worked.
    I've just tried it out with your example code now, with explicit <ResourceDictionary> it works by me, without it it doesn't.

    0 讨论(0)
提交回复
热议问题