WPF自定义圆角按钮嵌入ElementHost后偶尔无响应问题
问题描述
开发PowerPoint插件时,UI通过WinForm的ElementHost控件嵌入WPF实现。自定义圆角按钮外观显示正常,但偶尔出现鼠标移入无响应的情况:hover/press事件不触发,点击时会选中后方的父元素,如同按钮处于“幽灵状态”。移除模板中实现圆角的Border后问题消失,默认按钮无此问题;但自定义按钮获得焦点时功能完全正常,推测可能与性能相关。
承载按钮的UserControl内Grid代码
<Viewbox Grid.Column="2" Grid.RowSpan="3" HorizontalAlignment="Right" Margin="0,5,10,5"> <Grid Height="100" Width="100"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Grid.Row="0" Grid.Column="0" Background="red" Style="{StaticResource AnchorPoint}" Click="Button_Click_1"/> <!--<Button Grid.Row="0" Grid.Column="1" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="0" Grid.Column="2" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="1" Grid.Column="2" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="1" Grid.Column="1" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="2" Grid.Column="0" Style="{StaticResource AnchorPoint}"/> <Button Grid.Row="2" Grid.Column="1" Style="{StaticResource AnchorPoint}"/>--> <Button Grid.Row="2" Grid.Column="2" Style="{StaticResource AnchorPoint}"/> <Button Content="Button" Grid.Column="2" Grid.Row="1"/> </Grid> </Viewbox>
按钮样式代码
<Style TargetType="Button" x:Key="AnchorPoint"> <Setter Property="Background" Value="Transparent"></Setter> <Setter Property="Foreground" Value="Transparent"></Setter> <Setter Property="BorderBrush" Value="{x:Null}"></Setter> <Setter Property="BorderThickness" Value="0"></Setter> <Setter Property="HorizontalAlignment" Value="Stretch"></Setter> <Setter Property="VerticalAlignment" Value="Stretch"></Setter> <Setter Property="Margin" Value="0,0,0,0"></Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border x:Name="border" CornerRadius="2" Margin="0" BorderThickness="2" Style="{StaticResource TestHidden}"> <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Content"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" Value="Red" TargetName="border"></Setter> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="blue" TargetName="border"></Setter> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="green" TargetName="border"></Setter> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
Border样式代码
<Style x:Key="TestHidden" TargetType="Border"> <Setter Property="BorderBrush" Value="{StaticResource SkyGrey}"/> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="Focusable" Value="False"></Setter> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="{StaticResource SkyGrey}" /> <Setter Property="Background" Value="{StaticResource SkyGreyHover}" /> </Trigger> </Style.Triggers> </Style>
内容的提问来源于stack exchange,提问作者ImCattastic
相关产品推荐
相关产品推荐

