滚动条中的 WPF 重复按钮仅触发模板中的 IsPressed,而不触发样式

发布于 2024-09-12 07:15:50 字数 2320 浏览 2 评论 0原文

我有一个小问题。我们希望在样式中和控件模板之外放置尽可能多的样式项,以使主题更容易。因此,对于滚动条的重复按钮,我可以让所有这些都起作用,但 IsPressed 除外。这只适用于模板。

所以模板是(基本上):

    <ControlTemplate x:Key="ScrollBarButtonCT" TargetType="{x:Type RepeatButton}">
    <Border 
        x:Name="borderRepeatButton"
        Margin="1" 
        CornerRadius="2" 
        Background="{TemplateBinding Background}">
        <Path x:Name="pathArrow"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Fill="{DynamicResource ThumbBrush}"
            Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="true">
            <Setter TargetName="borderRepeatButton" Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

样式是

    <Style x:Key="ScrollBarButtonStyle" TargetType="{x:Type RepeatButton}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="IsTabStop" Value="false"/>
    <Setter Property="Background" Value="{DynamicResource ScrollBarBGBrush}"/> <!-- borderRepeatButton -->
    <Setter Property="OpacityMask" Value="{DynamicResource ThumbBrush}"/> <!-- pathArrow-->
    <Setter Property="Template" Value="{StaticResource ScrollBarButtonCT}"/>
    <Style.Triggers>
        <!--<Trigger Property="IsPressed" Value="true">  .... this doesn't work coming from the style
            <Setter Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
        </Trigger>-->
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{DynamicResource ScrollBarDisabledBGBrush}"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="{DynamicResource ThumbHoverBrush}"/>
        </Trigger>
    </Style.Triggers>
</Style>

我无法让 IsPressed 从样式中工作。使用该控件时,在 Snoop 中查看 IsPressed 会很好地提高。我做错了什么?谢谢!

I have a minor issue. We'd like to put as much stylistic items in the styles and outside of the control templates, to make themeing easier. So for the scrollbar's repeatbutton, I can get all of this to work but IsPressed. That only works from the template.

So the template is (basically):

    <ControlTemplate x:Key="ScrollBarButtonCT" TargetType="{x:Type RepeatButton}">
    <Border 
        x:Name="borderRepeatButton"
        Margin="1" 
        CornerRadius="2" 
        Background="{TemplateBinding Background}">
        <Path x:Name="pathArrow"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Fill="{DynamicResource ThumbBrush}"
            Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="true">
            <Setter TargetName="borderRepeatButton" Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

And the style is

    <Style x:Key="ScrollBarButtonStyle" TargetType="{x:Type RepeatButton}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Focusable" Value="false"/>
    <Setter Property="IsTabStop" Value="false"/>
    <Setter Property="Background" Value="{DynamicResource ScrollBarBGBrush}"/> <!-- borderRepeatButton -->
    <Setter Property="OpacityMask" Value="{DynamicResource ThumbBrush}"/> <!-- pathArrow-->
    <Setter Property="Template" Value="{StaticResource ScrollBarButtonCT}"/>
    <Style.Triggers>
        <!--<Trigger Property="IsPressed" Value="true">  .... this doesn't work coming from the style
            <Setter Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
        </Trigger>-->
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="{DynamicResource ScrollBarDisabledBGBrush}"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="true">
            <Setter Property="Background" Value="{DynamicResource ThumbHoverBrush}"/>
        </Trigger>
    </Style.Triggers>
</Style>

I can't get IsPressed to work from the style. Looking in Snoop IsPressed is raised just fine when using the control. What am I doing wrong? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦巷 2024-09-19 07:15:50

不知道为什么它不起作用,也许它需要静态资源?你可以尝试这个将所有样式集中到一个地方。

    <Style x:Key="xxxtyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="rectangle" Value="#FFD5D5D5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ps TargetType="typeName" == TargetType="{x:Type typename}"

No idea why it doesnt work, maybe it needs static resource? u can try this to get all styles in one place.

    <Style x:Key="xxxtyle" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="rectangle" Value="#FFD5D5D5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ps TargetType="typeName" == TargetType="{x:Type typename}"

寄居者 2024-09-19 07:15:50

我知道这已经很旧了,但事实证明这一定是模板中的错误。我们永远无法让它发挥作用,与内部一些人的交谈或多或少证实了这一点。我们只是将值保留在模板中,并在需要不同的 RepeatButton 样式时通过交换模板来解决它。

I know this is old, but it turns out this must be a bug in the template. We could never get it to work, and talking to some people on the inside more or less confirmed it. We just left the value in the template and worked around it by swapping templates when we needed a different RepeatButton style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文