在RichTextBox中,Border.IsMouseOver (在RichTextBox中的InlineUIContainer不响应事件/触发器)

3

假设我有以下代码:

<Grid>

    <Grid.Resources>

        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="Border.IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Black" />
                    <Setter Property="BorderBrush" Value="Red" />
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="CornerRadius" Value="7,2,10,2" />
                </Trigger>
                <Trigger Property="Border.IsMouseOver" Value="false">
                    <Setter Property="Background" Value="Black" />
                    <Setter Property="BorderBrush" Value="RoyalBlue" />
                    <Setter Property="BorderThickness" Value="2" />
                    <Setter Property="CornerRadius" Value="7,2,10,2" />
                </Trigger>
            </Style.Triggers>

        </Style>

        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="7,0,7,1" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Foreground" Value="White" />
        </Style>

        <Style TargetType="{x:Type Run}">
            <Setter Property="FontSize" Value="12" />
        </Style>

    </Grid.Resources>

        <FlowDocument >

            <Paragraph>

                <Span>

                    <Border>

                        <TextBlock>Test</TextBlock>

                    </Border>

                </Span>

            </Paragraph>

        </FlowDocument>

    </RichTextBox>

</Grid>

当边框在RichTextBox之外时,样式触发器的效果非常好,但是当它们在RichTextBox的InlineUIContainer中时,效果就不太好了。

我可以通过在代码后台使用MouseOver事件和VisualTreeHelper.HitTest()方法来设置属性以获得所需的行为,但我很确定这样做非常低效,而且不能帮助我找到更好的方法来解决这个问题?

如果有人能在这方面提供一些指导,那将不胜感激。

1个回答

2

我不得不在互联网的最深处搜寻,才找到了这个方法,但看起来有一种黑客方法可以启用FlowDocument中的InlineUIElements事件:

public class EventEnabledFlowDocument : FlowDocument
{
    protected override bool IsEnabledCore
    {
        get
        {
            return true;
        }
    }
}

请注意,这样做可能会产生一些不良副作用,但对于我的目的来说似乎有效。我知道其中一个副作用是 - 如果您删除InlineUIElement然后撤消该删除操作,则不会保存事件处理程序。

另一个副作用是,如果您将文档保存/加载为XAML字符串,则该属性无法起作用!有什么解决方法吗? - tkefauver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接