当IsFocused == true

时间:2016-04-29 09:38:13

标签: c# wpf xaml frontend

我的VerticalScrollBar中有ListBox。我想创建一个触发器,当他和周围的区域(1-2像素)聚焦时,会增加VerticalScrollBar的宽度。

我尝试这样做,但它无法正常工作:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type ScrollBar}" >
        <Style.Resources>
            <Style x:Key="ScrollBarThumbStyle" TargetType="{x:Type Thumb}">
                <Setter Property="IsTabStop" Value="False"/>
                <Setter Property="Focusable" Value="True"/>

                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Thumb}">
                            <Rectangle Fill="#36B448" />
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
      <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}">               
                <Grid Background="#FFB1B1B1" Width="5" >

                    <Track Name="PART_Track" IsDirectionReversed="True">
                        <Track.Thumb>
                            <Thumb Style="{StaticResource ScrollBarThumbStyle}" />
                        </Track.Thumb>
                    </Track>
                </Grid>
            </ControlTemplate>
    <Style.Triggers>
                <Trigger Property="Orientation" Value="Horizontal">
                    <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}"/>
                </Trigger>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Width" Value="22" />
                </Trigger>

                <!--<Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="Width" Value="15"></Setter>
                </Trigger>-->
    </Style.Triggers>

我该怎么做?

我认为EventTriggers可以让它更容易......

1 个答案:

答案 0 :(得分:0)

您可以像这样创建自己的自定义滚动条

    /// <summary>
    /// My custom ScrollBar
    /// </summary>
    public class MyScrollBar:ScrollBar
    {

        /// <summary>
        /// Subscribe to the events you need
        /// </summary>
        public MyScrollBar()
        {
            MouseEnter += MyScrollBar_MouseEnter;
            MouseLeave += MyScrollBar_MouseLeave;
        }

        private void MyScrollBar_MouseEnter(object sender, MouseEventArgs e)
        {
            //Change the width here
            Width = 25.0;
        }

        private void MyScrollBar_MouseLeave(object sender, MouseEventArgs e)
        {
            //Change the width back to default value
            Width = ....
        }

不知道哪些事件适合你改变宽度只是尝试。 然后在你的xaml中使用MyScrollBar而不是ScrollBar。希望它有所帮助。