扩展样式以捕获单击事件

时间:2014-01-03 10:42:11

标签: wpf xaml

我正在努力建立像visual studio这样的设计师。

看看xaml:

<Style x:Key="myStyle" TargetType="{x:Type Border}">
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="2" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="DodgerBlue" />
        </Trigger>
    </Style.Triggers>
</Style>
...
...
...
<Border Style="myStyle">
    <Grid>
        <Border Style="myStyle">
            <Rectangle Fill="Transparent" />
            <TextBlock Text="abc" />
        </Border>        
    </Grid>
</Border>

以上代码运行正常。现在我想扩展上面的样式,以便当我点击任何一个控件时边框的颜色应该变为绿色

更新:

我已将上述样式更改为下面的代码。

<Style x:Key="BorderStyle" TargetType="{x:Type Border}">
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="2" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="DodgerBlue" />
        </Trigger>
        <EventTrigger RoutedEvent="MouseDown">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="BorderBrush.Color" To="Green" Duration="0:0:0.100" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Style.Triggers>
</Style>

现在,当我点击它时,我可以看到边框颜色变为绿色。但是当鼠标离开文本块时,边框会将其颜色更改为透明。

4 个答案:

答案 0 :(得分:0)

 <Style.Triggers>
       <EventTrigger RoutedEvent="MouseDown">
            <Setter Property="BorderBrush" Value="DodgerBlue" />                    
       </EventTrigger>
</Style.Triggers>

并且您不想绑定所有边框的样式,如果您需要为所有边框应用该样式,只需删除该样式的键名称即可。它适用于所有目标控制。

如果您需要检查并应用颜色,就像点击按钮一样,您需要维护一个属性,并且该属性将在触发器中给出,并且需要在按钮的单击中进行更改。 。

答案 1 :(得分:0)

试试这个

<Style x:Key="myStyle" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <SolidColorBrush x:Name="BorderBrushColor" Color="Transparent"></SolidColorBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="2" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter  Property="BorderBrush" Value="DodgerBlue" />
            </Trigger>  
            <EventTrigger RoutedEvent="MouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames  Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"  Value="Green" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>

答案 2 :(得分:0)

试试这个

   <Style x:Key="myStyle" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush">
            <Setter.Value>
                <SolidColorBrush x:Name="BorderBrushColor" Color="Transparent"></SolidColorBrush>
            </Setter.Value>
        </Setter>
        <Setter Property="BorderThickness" Value="2" />            
        <Style.Triggers>            
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames FillBehavior="HoldEnd" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"  Value="DodgerBlue" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseDown">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames FillBehavior="HoldEnd" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                            <EasingColorKeyFrame KeyTime="0"  Value="Green" />
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>               
        </Style.Triggers>
    </Style>

答案 3 :(得分:0)

以旧样式添加此代码

        <Trigger Property="IsMouseOver" Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimationUsingKeyFrames FillBehavior="HoldEnd" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                <EasingColorKeyFrame KeyTime="0"  Value="Transparent" />
                            </ColorAnimationUsingKeyFrames>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>                    
            </Trigger>