Wpf触发器无法识别控件以更改其属性

时间:2017-03-02 09:15:03

标签: wpf xaml triggers

我在这里尝试开发汉堡菜是菜单风格:

var bulk = db.collection.initializeUnorderedBulkOp(),   
    counter = 0;

cursor.forEach(function(doc){
    bulk.find({ "clientId": doc._id }).update({ "$set": { "clientId": doc.docId } });

    counter++;  // increment counter for batch limit
    if (counter % 500 === 0) { 
        // execute the bulk update operation in batches of 500
        bulk.execute(); 
        // Re-initialize the bulk update operations object
        bulk = db.collection.initializeUnorderedBulkOp();
    } 
})

// Clean up remaining operation in the queue
if (counter % 500 !== 0) { bulk.execute(); }

现在我的问题是如何在ToggleButton上触发MouseOver 这段代码不起作用,因为TargetName =" path"不被认可!!! :

<Style TargetType="local:HamburgerMenu">
    <Style.Resources>
        <!-- Fill Brushes -->
        <SolidColorBrush x:Key="DarkBrush" Color="#40000000" /> 

    </Style.Resources>
    <Setter Property="Width" Value="50"/>
    <Setter Property="Visibility" Value="Visible"/>
    <Setter Property="IsOpen" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:HamburgerMenu">                  
                <Grid x:Name="mainGrid" Background="{TemplateBinding Background}">
                    <ToggleButton  x:Name="menuIcon" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="50" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:HamburgerMenu}}, Path=IsOpen}">
                        <Path x:Name="path"  HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" Width="30" Fill="{TemplateBinding MenuIconColor}" Data="M2,15.5L22,15.5 22,17.5 2,17.5 2,15.5z M2,10.5L22,10.5 22,12.5 2,12.5 2,10.5z M2,5.5L22,5.5 22,7.5 2,7.5 2,5.5z"/>                          
                    </ToggleButton>                        
                    <ListBox ItemsSource="{TemplateBinding Content}" HorizontalAlignment="Left" Margin="0,40,0,0" VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectedIndex="0"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsOpen" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation 
                                     Storyboard.TargetProperty="Width"
                                     To="300"
                                     Duration="0:0:0.3" AccelerationRatio="0.1" DecelerationRatio="0.9"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation 
                                     Storyboard.TargetProperty="Width"
                                     To="50"
                                     Duration="0:0:0.2"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

并且此解决方案有效但不完美,因为它触发菜单的MouseOver而不仅仅是ToogleButton,因此当鼠标在菜单的任何位置结束时,ToogleButton的颜色会发生变化:

<ToggleButton.Triggers >
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="path" Property="Fill" Value="{StaticResource DarkBrush}" />
                            </Trigger>
                        </ToggleButton.Triggers>

2 个答案:

答案 0 :(得分:0)

您可以使用default template设置其Template。例如:

<ToggleButton  x:Name="menuIcon" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Width="50" > 
    <ToggleButton.Template>
        <ControlTemplate TargetType="ToggleButton">
            <Grid>
                <Border x:Name="Background" CornerRadius="3" Background="White"
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        BorderBrush="{TemplateBinding BorderBrush}">
                    <Grid Background="{TemplateBinding Background}"  Margin="1">
                        <Border Opacity="0"  x:Name="BackgroundAnimation" Background="#FF448DCA" />
                        <Rectangle x:Name="BackgroundGradient" >
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint=".7,0" EndPoint=".7,1">
                                    <GradientStop Color="#FFFFFFFF" Offset="0" />
                                    <GradientStop Color="#F9FFFFFF" Offset="0.375" />
                                    <GradientStop Color="#E5FFFFFF" Offset="0.625" />
                                    <GradientStop Color="#C6FFFFFF" Offset="1" />
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                    </Grid>
                </Border>
                <Path x:Name="path"  HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="Uniform" Width="30"
          Data="M2,15.5L22,15.5 22,17.5 2,17.5 2,15.5z M2,10.5L22,10.5 22,12.5 2,12.5 2,10.5z M2,5.5L22,5.5 22,7.5 2,7.5 2,5.5z"/>
                <Rectangle x:Name="DisabledVisualElement" RadiusX="3" RadiusY="3" Fill="#FFFFFFFF" Opacity="0" IsHitTestVisible="false" />
                <Rectangle x:Name="FocusVisualElement" RadiusX="2" RadiusY="2" Margin="1" Stroke="#FF6DBDD1" StrokeThickness="1" Opacity="0" IsHitTestVisible="false" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="path" Property="Fill" Value="Red" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ToggleButton.Template>
</ToggleButton>

请注意,您可能还需要更改其他一些内容。希望它有所帮助

答案 1 :(得分:0)

您可以将[1_Time].[Hierarchy].CurrentMember -> 3 period 2017 .Parent -> 2017 year .Lag(2) -> 2015 year .Children -> set {1 period 2015 year : 13 period 2015 year) .Item(0) -> 1 period 2015 year {[1_Time].[Hierarchy].CurrentMember.Parent.Lag(2).Children.Item(0): [1_Time].[Hierarchy].CurrentMember} -> set {1 period 2015 year : 3 period 2017} 的{​​{1}}属性绑定到Fill的{​​{1}}属性,然后将Path应用于Foreground在鼠标悬停时将ToggleButton属性设置为另一个Style

这应该有效:

ToggleButton