WPF:当父级鼠标悬停时,我可以设置 ChildElement 的 IsMouseOver 属性吗?

发布于 2024-10-01 17:55:33 字数 300 浏览 0 评论 0原文

假设我有一个嵌套在 ListBoxItemTemplate 中的按钮,我可以将按钮的 IsMouseOver 属性设置为 true,以便它看起来喜欢它的鼠标悬停吗?

仅供说明之用,我指的是窗口顶部的通知。它基本上是 ListBoxItemTextBlockButton

替代文字

Suppose I have a button nested in the Template of a ListBoxItem, can I set the IsMouseOver property of the button to true, so that it looks like its moused over?

Just for illustration, the notifications on the top of the window are what I am referring to. Its basically ListBoxItem's with a TextBlock and Button

alt text

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

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

发布评论

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

评论(1

以酷 2024-10-08 17:55:33

不幸的是,没有。 “IsMouseOver”是只读的。

不过,我假设您有一个按钮的自定义控件模板,对吧?如果是这种情况,一种解决方法是弄乱按钮的 Tag 属性。向 ControlTemplate 添加一个触发器,该触发器在设置特定标签值时触发。然后,在 ListBoxItems 的 DataTemplate 中,当项目上的 IsMouseOver 为 true 时,只需将按钮的 Tag 设置为该特定值。下面是一个例子:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel x:Name="dp" Background="Transparent">
                <Button x:Name="btn" DockPanel.Dock="Right" Content="x" Background="Gainsboro">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="bd" Padding="2" BorderBrush="Black" BorderThickness="1"
                                    Background="WhiteSmoke">
                                <ContentPresenter/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="bd" Property="Background" Value="LightBlue"/>
                                </Trigger>
                                <Trigger Property="Tag" Value="SimulatedMouseOver">
                                    <Setter TargetName="bd" Property="Background" Value="LightBlue"/>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter TargetName="bd" Property="Background" Value="Gray"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
                <TextBlock Text="{Binding}"/>
            </DockPanel>
            <DataTemplate.Triggers>
                <Trigger SourceName="dp" Property="IsMouseOver" Value="True">
                    <Setter TargetName="btn" Property="Tag" Value="SimulatedMouseOver"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <s:String>Item1</s:String>
    <s:String>Item2</s:String>
    <s:String>Item3</s:String>
</ListBox>

Unfortunately, no. "IsMouseOver" is readonly.

I'm assuming, though, that you have a custom control template for the Button, right? If that's the case, one workaround is to mess with the Tag property of the button. Add a trigger to the ControlTemplate that gets fired when a specific Tag value is set. Then, in the DataTemplate for your ListBoxItems, just set the button's Tag to that specific value when IsMouseOver on the item is true. Below is an example:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <DockPanel x:Name="dp" Background="Transparent">
                <Button x:Name="btn" DockPanel.Dock="Right" Content="x" Background="Gainsboro">
                    <Button.Template>
                        <ControlTemplate TargetType="Button">
                            <Border x:Name="bd" Padding="2" BorderBrush="Black" BorderThickness="1"
                                    Background="WhiteSmoke">
                                <ContentPresenter/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="bd" Property="Background" Value="LightBlue"/>
                                </Trigger>
                                <Trigger Property="Tag" Value="SimulatedMouseOver">
                                    <Setter TargetName="bd" Property="Background" Value="LightBlue"/>
                                </Trigger>
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter TargetName="bd" Property="Background" Value="Gray"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
                <TextBlock Text="{Binding}"/>
            </DockPanel>
            <DataTemplate.Triggers>
                <Trigger SourceName="dp" Property="IsMouseOver" Value="True">
                    <Setter TargetName="btn" Property="Tag" Value="SimulatedMouseOver"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <s:String>Item1</s:String>
    <s:String>Item2</s:String>
    <s:String>Item3</s:String>
</ListBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文