DynamicResource仅用于一个绑定?

时间:2015-05-17 08:59:52

标签: wpf xaml

我创造风格。此样式创建导航箭头

<Style TargetType="MenuItem" x:Key="navigation_arrow">
    <Setter Property="Icon">
        <Setter.Value>
            <DockPanel LastChildFill="True" HorizontalAlignment="Center" VerticalAlignment="Center">
                <DockPanel.LayoutTransform>
                    <RotateTransform Angle="0"/>
                </DockPanel.LayoutTransform>
                <Grid>
                <Canvas>
                    <Canvas.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.5" ScaleY="0.5"/>
                        </TransformGroup>
                    </Canvas.RenderTransform>
                    <Rectangle Fill="LightBlue" Width="24" Height="6" Canvas.Left="2" Canvas.Top="1" RenderTransformOrigin="0,0">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform Angle="30.277"/>
                                <TranslateTransform X="-0.79314" Y="-2.18035"/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                    <Rectangle Fill="LightBlue" Width="44" Height="6" Canvas.Left="2" Canvas.Top="-1" RenderTransformOrigin="0.5,0.5"/>
                    <Polygon Fill="LightBlue" Points="0,12, 12,12, 6,1.6" Canvas.Left="-7" Canvas.Top="-4" RenderTransformOrigin="0,0.5">
                        <Polygon.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform Angle="-89.8"/>
                                <TranslateTransform X="5.97906" Y="5.99996"/>
                            </TransformGroup>
                        </Polygon.RenderTransform>

                    </Polygon>
                    <Rectangle Fill="LightBlue" Width="24" Height="6" Canvas.Left="-1" Canvas.Top="2" RenderTransformOrigin="1,0">
                        <Rectangle.RenderTransform>
                            <TransformGroup>
                                <ScaleTransform/>
                                <SkewTransform/>
                                <RotateTransform Angle="-30.277"/>
                                <TranslateTransform X="-4.00853" Y="-14.1804"/>
                            </TransformGroup>
                        </Rectangle.RenderTransform>
                    </Rectangle>
                </Canvas>
                </Grid>
            </DockPanel>
        </Setter.Value>
    </Setter>
</Style>

然后我将此样式设置为MenuItem

<MenuItem Style="{DynamicResource navigation_arrow}" Command="NavigationCommands.BrowseBack" />
<MenuItem Style="{DynamicResource navigation_arrow}" Command="NavigationCommands.BrowseForward" />

我看到第一个MenuItem丢失了hist样式并变空了。但第二个MenuItem应用这种风格。如何解决这个问题?

enter image description here

1 个答案:

答案 0 :(得分:1)

我现在看到的唯一解决方案,添加x:Shared="False"

    <Style x:Key="navigation_arrow"
           TargetType="Button"
           x:Shared="False">
     ...

你的风格。