【问题标题】:Problem Customizing the Navigation Menu from Silverlight Business Application从 Silverlight 业务应用程序自定义导航菜单的问题
【发布时间】:2011-09-26 18:39:47
【问题描述】:

我正在使用 Silverlight 业务应用程序模板,但我需要自定义 MainPage 布局以匹配客户端现有的 ASP.NET 项目模式。我能够创建突出显示所选项目的导航菜单。但是当一个 MouseEnter 事件发生时,被选中的菜单项的样式就变成了 MouseOver VisualState 的样式。在 MouseLeave 上,选定的菜单项变为 Normal VisualState。此行为适用于解决方案的默认菜单,但我的修改可能缺少某些内容。代码定义如下。谢谢!

<Style x:Key="NavMenuItem" TargetType="HyperlinkButton">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontSize" Value="11"/>
    <Setter Property="FontWeight" Value="Bold" />
    <Setter Property="Padding" Value="20,10,20,10" />
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="MinHeight" Value="40"/>
    <Setter Property="MinWidth" Value="150"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="HyperlinkButton">
                <Grid Margin="20,20,0,0">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation BeginTime="0" To="1" Duration="00:00:00.01" 
                                                     Storyboard.TargetName="MenuItemBorder" 
                                                     Storyboard.TargetProperty="Opacity" />
                                    <DoubleAnimation BeginTime="0" To="1" Duration="00:00:00.01" 
                                                     Storyboard.TargetName="MenuItemBorder" 
                                                     Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.ShadowDepth)" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <DoubleAnimation BeginTime="0" To="1" Duration="00:00:00.01" 
                                                     Storyboard.TargetName="MenuItemBorder" 
                                                     Storyboard.TargetProperty="Opacity" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="LinkStates">
                            <VisualState x:Name="InactiveLink"/>
                            <VisualState x:Name="ActiveLink">
                                <Storyboard>
                                    <DoubleAnimation BeginTime="0" To="1" Duration="00:00:00.01" 
                                                     Storyboard.TargetName="MenuItemBorder" 
                                                     Storyboard.TargetProperty="Opacity" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border Name="MenuItemBorder" CornerRadius="10,0,0,10" Opacity="0" 
                            MinHeight="{TemplateBinding MinHeight}" MinWidth="{TemplateBinding MinWidth}">
                        <!--BorderBrush="Silver" BorderThickness="1"-->
                        <Border.Background>
                            <LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5, 1">
                                <GradientStop Color="White" Offset="0.6"/>
                                <GradientStop Color="Silver" Offset="1"/>
                            </LinearGradientBrush>
                        </Border.Background>
                        <Border.Effect>
                            <DropShadowEffect x:Name="BorderShadow" ShadowDepth="5" Color="#FF484848" Opacity="0.5" BlurRadius="5"/>
                        </Border.Effect>
                    </Border>
                    <ContentPresenter x:Name="ContentPresenter" 
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                          Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
            <HyperlinkButton Style="{StaticResource NavMenuItem}" NavigateUri="/Home" TargetName="ContentFrame" 
                             MouseEnter="HyperlinkButton_MouseEnterLeave" MouseLeave="HyperlinkButton_MouseEnterLeave" />
    private void HyperlinkButton_MouseEnterLeave(object sender, MouseEventArgs e) {
        HyperlinkButton hb = sender as HyperlinkButton;
        if (hb.NavigateUri.ToString() == ContentFrame.CurrentSource.ToString())
            VisualStateManager.GoToState(hb, "ActiveLink", true);
    }

【问题讨论】:

    标签: c# silverlight xaml menu navigation


    【解决方案1】:

    如果事件是 MouseEnter,也许你可以尝试在 EventHandler 上设置按钮的 MouseOver VisualSatate,如果它是 MouseLeave 事件,则重置它。

    我想我会设置两个事件处理程序,而不仅仅是一个用于两个事件,然后您也可以轻松设置 MouseOver 状态和 ActiveState。

    【讨论】:

    • 感谢您的即时回复。当鼠标进入或离开时,我实际上希望选定的菜单保持在 ActiveLink VisualState 上。但是当前的行为是选中的菜单在 MouseEnter 上更改为 MouseOver VisualState,然后在 MouseLeave 上更改为 Normal VisualState。客户端行为似乎覆盖了我从后面的代码中设置的 VisualState。
    • 哦,现在我终于明白你的问题了。 :)
    • 没关系。 :) 对我来说更难,因为我的英语不是我的母语。我猜这两个故事板都是为您的按钮开始的,ActiveLink 首先完成,然后完成 MouseLeave,也许正因为如此,它会回到正常状态。您可以尝试扩展情节提要的时间段并查看它们的实际运行情况。
    • 我尝试将 ActiveLink 持续时间增加到 0.05 秒,但结果仍然相同。顺便说一句,LinkState 不是 HyperlinkBut​​ton 的标准 ViewStateGroups 的一部分,它仅使用以下代码进行设置:
    • 是的,我从您的代码和 XAML 中看到,疯狂的猜测是从 CommonStates 组中删除正常和 MouseOver 视觉状态并将它们添加到您的链接状态,然后从您的事件处理程序中手动更改它们。这是我现在能想到的最好的了,但我会快速浏览一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多