Wpf 基于资源字典内样式的按钮样式

Wpf 基于资源字典内样式的按钮样式,wpf,xaml,Wpf,Xaml,我在Window.Resources中有一个带有键BtnHyperLinkStyle的资源字典。我不能删除键,因为它说每本字典都应该有一个键。问题是,当我说BasedOn时,在一个样式标记上,它给了我一个错误,说它找不到名为“HyperLinkButtonStyle”的样式,即使该样式存在于StylesButton.xaml中。请帮助。ResourceDictionary源是正确的 <Window.Resources> <ResourceDictionary x:Key="Bt

我在Window.Resources中有一个带有键BtnHyperLinkStyle的资源字典。我不能删除键,因为它说每本字典都应该有一个键。问题是,当我说BasedOn时,在一个样式标记上,它给了我一个错误,说它找不到名为“HyperLinkButtonStyle”的样式,即使该样式存在于StylesButton.xaml中。请帮助。ResourceDictionary源是正确的

<Window.Resources>
<ResourceDictionary x:Key="BtnHyperLinkStyle">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>

<Style x:Key="btnFlashButtonEnabledStyle" TargetType="Button" BasedOn="{StaticResource HyperLinkButtonStyle}"> <!--error here. it cant find the key-->
            <Setter Property="Content">
                <Setter.Value>
                    <Viewbox Height="40">
                        <Canvas Width="10240" Height="10240">
                            <Path Data="M6720 7533l0 1427c0,353 -287,640 -640,640l0 0 0 0 0 0c0,176 -216,320 -480,320l-960 0c-264,0 -480,-144 -480,-320l0 0 0 0 0 0c-353,0 -640,-287 -640,-640l0 -1427c139,49 286,78 427,98 382,53 787,49 1173,49 386,0 791,4 1173,-49 141,-20 288,-49 427,-98z" Fill="#252525"/>
                            <Path Data="M5120 320c1624,0 3200,1165 3200,2880 0,1600 -1280,2240 -1280,3200l0 320c0,640 -960,640 -1920,640 -960,0 -1920,0 -1920,-640l0 -320c0,-960 -1280,-1600 -1280,-3200 0,-1711 1572,-2880 3200,-2880zm642 715c-211,-49 -427,-75 -642,-75 -1266,0 -2560,884 -2560,2240 0,303 64,569 164,813 35,86 97,146 184,179 87,32 173,28 256,-14 149,-75 216,-252 153,-407 -73,-177 -117,-363 -117,-571 0,-994 1016,-1600 1920,-1600 164,0 331,20 496,58 146,35 292,-34 359,-168l0 -1c46,-90 47,-187 4,-279 -43,-91 -119,-152 -217,-175z" Fill="#A1C057"/>
                        </Canvas>
                    </Viewbox>
                </Setter.Value>
            </Setter>
        </Style>
</Window.Resources>

您可能想试试这个。将
样式
移动到
资源字典
中,并去掉
x:Key

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="btnFlashButtonEnabledStyle" TargetType="Button" BasedOn="{StaticResource HyperLinkButtonStyle}">
            <Setter Property="Content">
                <Setter.Value>
                    <Viewbox Height="40">
                        <Canvas Width="10240" Height="10240">
                            <Path Data="M6720 7533l0 1427c0,353 -287,640 -640,640l0 0 0 0 0 0c0,176 -216,320 -480,320l-960 0c-264,0 -480,-144 -480,-320l0 0 0 0 0 0c-353,0 -640,-287 -640,-640l0 -1427c139,49 286,78 427,98 382,53 787,49 1173,49 386,0 791,4 1173,-49 141,-20 288,-49 427,-98z" Fill="#252525"/>
                            <Path Data="M5120 320c1624,0 3200,1165 3200,2880 0,1600 -1280,2240 -1280,3200l0 320c0,640 -960,640 -1920,640 -960,0 -1920,0 -1920,-640l0 -320c0,-960 -1280,-1600 -1280,-3200 0,-1711 1572,-2880 3200,-2880zm642 715c-211,-49 -427,-75 -642,-75 -1266,0 -2560,884 -2560,2240 0,303 64,569 164,813 35,86 97,146 184,179 87,32 173,28 256,-14 149,-75 216,-252 153,-407 -73,-177 -117,-363 -117,-571 0,-994 1016,-1600 1920,-1600 164,0 331,20 496,58 146,35 292,-34 359,-168l0 -1c46,-90 47,-187 4,-279 -43,-91 -119,-152 -217,-175z" Fill="#A1C057"/>
                        </Canvas>
                    </Viewbox>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>

您可能想试试这个。将
样式
移动到
资源字典
中,并去掉
x:Key

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="btnFlashButtonEnabledStyle" TargetType="Button" BasedOn="{StaticResource HyperLinkButtonStyle}">
            <Setter Property="Content">
                <Setter.Value>
                    <Viewbox Height="40">
                        <Canvas Width="10240" Height="10240">
                            <Path Data="M6720 7533l0 1427c0,353 -287,640 -640,640l0 0 0 0 0 0c0,176 -216,320 -480,320l-960 0c-264,0 -480,-144 -480,-320l0 0 0 0 0 0c-353,0 -640,-287 -640,-640l0 -1427c139,49 286,78 427,98 382,53 787,49 1173,49 386,0 791,4 1173,-49 141,-20 288,-49 427,-98z" Fill="#252525"/>
                            <Path Data="M5120 320c1624,0 3200,1165 3200,2880 0,1600 -1280,2240 -1280,3200l0 320c0,640 -960,640 -1920,640 -960,0 -1920,0 -1920,-640l0 -320c0,-960 -1280,-1600 -1280,-3200 0,-1711 1572,-2880 3200,-2880zm642 715c-211,-49 -427,-75 -642,-75 -1266,0 -2560,884 -2560,2240 0,303 64,569 164,813 35,86 97,146 184,179 87,32 173,28 256,-14 149,-75 216,-252 153,-407 -73,-177 -117,-363 -117,-571 0,-994 1016,-1600 1920,-1600 164,0 331,20 496,58 146,35 292,-34 359,-168l0 -1c46,-90 47,-187 4,-279 -43,-91 -119,-152 -217,-175z" Fill="#A1C057"/>
                        </Canvas>
                    </Viewbox>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>

为了引用
资源字典中的资源,
样式
必须是同一
资源字典
的一部分。基本上,您的对象树是错误的。当您使用
ResourceDictionary
作为
Resources
属性的内容时,它应该是唯一的内容:

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="btnFlashButtonEnabledStyle" 
           TargetType="Button" 
           BasedOn="{StaticResource HyperLinkButtonStyle}">
    </Style>
  </ResourceDictionary>
</Window.Resources>

为了引用
资源字典中的资源,
样式
必须是同一
资源字典
的一部分。基本上,您的对象树是错误的。当您使用
ResourceDictionary
作为
Resources
属性的内容时,它应该是唯一的内容:

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="ms-appx:///Presentation.Common/Styles/StylesButton.xaml"/>
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="btnFlashButtonEnabledStyle" 
           TargetType="Button" 
           BasedOn="{StaticResource HyperLinkButtonStyle}">
    </Style>
  </ResourceDictionary>
</Window.Resources>