重复按钮不会失去焦点,并永远突出显示

时间:2011-11-23 14:13:22

标签: .net wpf focus repeatbutton

我正在使用我的Up Down控件,它工作正常,但今天我注意到控制中重复按钮的一个奇怪问题。一旦我点击+/-重复按钮,它们就会以蓝色边框突出显示(很好),但问题是即使我点击页面上的其他按钮或控件,它们仍会保持高亮显示。

这是截图 -

enter image description here

这是我正在使用的xaml -

<!--  RepeatButton styles  -->

<Style
    x:Key="RepeatButtonPathStyle"
    TargetType="Path">
    <Setter
        Property="Width"
        Value="3" />
    <Setter
        Property="Height"
        Value="3" />
    <Setter
        Property="MinWidth"
        Value="3" />
    <Setter
        Property="MinHeight"
        Value="3" />
    <Setter
        Property="HorizontalAlignment"
        Value="Center" />
    <Setter
        Property="VerticalAlignment"
        Value="Center" />
    <Setter
        Property="Stretch"
        Value="None" />
    <Setter
        Property="StrokeThickness"
        Value="1" />
    <Setter
        Property="Stroke"
        Value="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton},
Path=Foreground}" />
</Style>

<DataTemplate
    x:Key="IncreaseGlyph">
    <Path
        Data="M0,1.5 H3 M1.5,0 V3"
        Style="{StaticResource RepeatButtonPathStyle}" />
</DataTemplate>

<DataTemplate
    x:Key="DecreaseGlyph">
    <Path
        Data="M0,1.5 H3"
        Style="{StaticResource RepeatButtonPathStyle}" />
</DataTemplate>

<Grid
    Grid.Column="1">
    <Grid.RowDefinitions>
        <RowDefinition
            Height="*" />
        <RowDefinition
            Height="*" />
    </Grid.RowDefinitions>
    <Button
        Grid.Row="0"
        Grid.RowSpan="2"
        IsHitTestVisible="True"
        IsTabStop="False">
        <Button.Template>
            <ControlTemplate
                TargetType="Button">
                <Grid
                    Background="Transparent" />
            </ControlTemplate>
        </Button.Template>
    </Button>
    <RepeatButton
        Grid.Row="0"
        HorizontalContentAlignment="Center"
        Command="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=IncreaseCommand}"
        ContentTemplate="{StaticResource IncreaseGlyph}"
        ToolTip="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=IncreaseButtonToolTip}"
        ToolTipService.BetweenShowDelay="1000"
        ToolTipService.InitialShowDelay="1000"
        ToolTipService.IsEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=ShowUpDownButtonToolTip}">
    </RepeatButton>
    <RepeatButton
        Grid.Row="1"
        HorizontalContentAlignment="Center"
        Command="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=DecreaseCommand}"
        ContentTemplate="{StaticResource DecreaseGlyph}"
        ToolTip="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=DecreaseButtonToolTip}"
        ToolTipService.BetweenShowDelay="1000"
        ToolTipService.InitialShowDelay="1000"
        ToolTipService.IsEnabled="{Binding RelativeSource={RelativeSource TemplatedParent}, 
            Path=ShowUpDownButtonToolTip}">
    </RepeatButton>
</Grid>

任何指针?

2 个答案:

答案 0 :(得分:0)

我认为你应该替换这个二传手:

<Setter Property="Stroke" Value="{Binding RelativeSource=
{RelativeSource AncestorType=RepeatButton},Path=Foreground}" />

使用触发器代替:

<Style.Triggers>
  <Trigger Property="IsMouseOver" Value="true">
    <Setter Property="Stroke" 
            Value="{Binding RelativeSource={RelativeSource AncestorType=RepeatButton},
                      Path=Foreground}" />
  </Trigger>
</Style.Triggers>

这样,当你的触发条件为假(在这种情况下鼠标移出)时,它会将笔画设置回默认值。

答案 1 :(得分:0)

我找到解决此问题的唯一方法是在RepeatButton上设置Focusable="False"