为什么忽略添加BorderBrush?

时间:2012-12-31 16:48:35

标签: xaml windows-phone-8

我改变了我的超链接:

   <HyperlinkButton x:Name="hyperlinkButtonManageInvitations" Margin="24"
                    Grid.Row="1" Tap="HyperlinkButtonManageInvitations_OnTap">
       Manage Invitations
   </HyperlinkButton>

......对此:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations"
                     BorderBrush="DarkOrange" Margin="24" Grid.Row="1"
                     Tap="HyperlinkButtonManageInvitations_OnTap">
        Manage Invitations
    </HyperlinkButton>

...并且没有视觉变化 - 没有添加边框。所以我接着尝试调整了一些更多的属性:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations"
                     BorderBrush="DarkOrange" Margin="24" Grid.Row="1"
                     Tap="HyperlinkButtonManageInvitations_OnTap"
                     BorderThickness="4" OpacityMask="Black">
        Manage Invitations
    </HyperlinkButton>

......但仍然没有去。添加边框的哪一部分我不是在闲聊?

更新

我的超链接按钮现在是:

    <HyperlinkButton x:Name="hyperlinkButtonManageInvitations" Style="{StaticResource BorderedHyperlinkButtonStyle}" BorderThickness="5" BorderBrush="Red" Foreground="DarkOrange" Margin="24" Grid.Row="1" Tap="HyperlinkButtonManageInvitations_OnTap" Content="Manage Invitations">
    </HyperlinkButton>

...我在App.xaml的部分添加了下面提供的样式,但我仍然看到现在有问题的超链接按钮边框。

更新2

它现在在那里 - 它“突然”出现了;我猜xaml渲染器可能会慢慢吸收。或者在使用新添加的样式时,需要首先重新加载项目,或者...... ???

1 个答案:

答案 0 :(得分:5)

这与HyperlinkBut​​ton控件模板有关;如果你去混合并创建一个控件模板的副本进行编辑,你会得到这样的东西:

<ControlTemplate TargetType="HyperlinkButton">
  <Border Background="Transparent">
    <VisualStateManager.VisualStateGroups>
      <VisualStateGroup x:Name="CommonStates">
        <VisualState x:Name="Normal"/>
        <VisualState x:Name="MouseOver"/>
        <VisualState x:Name="Pressed">
          <Storyboard>
            <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextElement"/>
          </Storyboard>
        </VisualState>
        <VisualState x:Name="Disabled">
          <Storyboard>
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
            </ObjectAnimationUsingKeyFrames>
          </Storyboard>
        </VisualState>
      </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <Border Background="{TemplateBinding Background}" Margin="{StaticResource PhoneHorizontalMargin}" Padding="{TemplateBinding Padding}">
      <TextBlock x:Name="TextElement" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Text="{TemplateBinding Content}" TextDecorations="Underline" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Border>
  </Border>
</ControlTemplate>

所以你可以在这里检查控件BorderBrush和BorderThickness属性没有TemplateBinding。

要解决您的问题,只需复制此样式并将其插入页面/应用资源:

<Style x:Key="BorderedHyperlinkButtonStyle" TargetType="HyperlinkButton">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="HyperlinkButton">
        <Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CommonStates">
              <VisualState x:Name="Normal"/>
              <VisualState x:Name="MouseOver"/>
              <VisualState x:Name="Pressed">
                <Storyboard>
                  <DoubleAnimation Duration="0" To="0.5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextElement"/>
                </Storyboard>
              </VisualState>
              <VisualState x:Name="Disabled">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="TextElement">
                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>
            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <Border Background="{TemplateBinding Background}" Margin="{StaticResource PhoneHorizontalMargin}" Padding="{TemplateBinding Padding}">
            <TextBlock x:Name="TextElement" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Text="{TemplateBinding Content}" TextDecorations="Underline" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
          </Border>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

然后,在你想要的带有边框的HyperlinkBut​​ton上,设置它是这样的:

<HyperlinkButton Style="{StaticResource BorderedHyperlinkButtonStyle}" BorderThickness="5" BorderBrush="Red" />

根据需要编辑BorderThickness和BorderBrush!