模拟一个长鼠标在wpf上

本文关键字:鼠标 wpf 一个 模拟 | 更新日期: 2023-09-27 18:19:02

我有一个datagridtemplatecoluml中包含超链接的数据网格,如下所示:

<DataGridTemplateColumn Width="170" SortMemberPath="Joueur.EtatCivil.Joueur_nom"  CanUserReorder="False" CanUserResize="True" Header="">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate />
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource DatagridCellHyperlinkStyle}" >
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border Padding="{TemplateBinding Padding}" Width="Auto"  VerticalAlignment="Center" SnapsToDevicePixels="True">
                            <TextBlock Foreground="{TemplateBinding Foreground}" Width="Auto" TextTrimming="CharacterEllipsis">
                        <Hyperlink IsEnabled="{TemplateBinding IsEnabled}">
                         <InlineUIContainer TextDecorations="{Binding Path=TextDecorations, RelativeSource={RelativeSource AncestorType=TextBlock}}" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType=TextBlock}}">
                          <ContentPresenter Content="{Binding DataContext.Joueur.EtatCivil.NomComplet, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
                         </InlineUIContainer>
                            <Hyperlink.Style>
                                <Style TargetType="Hyperlink" BasedOn="{StaticResource HyperlinkStyle}">
                                    <EventSetter Event="Hyperlink.Click" Handler="ClickEvent" />
                                    <EventSetter Event="Hyperlink.MouseEnter" Handler="MouseOverEvent" />
                                    <EventSetter Event="Hyperlink.MouseLeave" Handler="ClicJoueurMouseLeaveEvent" />                                        
                                </Style>
                            </Hyperlink.Style>
                        </Hyperlink>
                            </TextBlock>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>

这工作得很好,正如预期的那样,但是当我在很短的时间内(<0.5s可能)移动光标悬停许多行时,我遇到了麻烦。

我的MouseEnterEvent调用一个名为"Show();"的方法,我的MouseLeaveEvent调用一个名为"Hide();"

这个显示/隐藏的小弹出显示给用户更多的数据。问题是我的show();方法更新Wpf工具箱图表,该图表似乎无法以这种频率更新(0.5s内更新10次)。绑定似乎"崩溃"了,再也不起作用了。绑定更新将新闻系列添加到WPF工具包图表(而不是替换/更新系列))

我这样更新我的图表:

this.colonnes.ItemsSource = null;
                _DataGraph = new ObservableCollection<GraphiqueValeurs>();
                _DataGraph.Add(new GraphiqueValeurs(value, "my string");
                this.colonnes.ItemsSource = _DataGraph;

                this.colonnes.UpdateLayout();

和下面的xaml:

<chartingToolkit:Chart  VerticalAlignment="Top" HorizontalAlignment="Center" FontFamily="/BPM2015;component/#Open Sans Condensed" BorderBrush="Transparent" Name="columnChart" Title="Column Series Demo" Foreground="{DynamicResource CouleurTexte}" >
            <chartingToolkit:ColumnSeries Name="colonnes" Foreground="{DynamicResource CouleurTexte}"  DependentValueBinding="{Binding Valeur}"
        IndependentValueBinding="{Binding Name}"
        DataPointStyle="{StaticResource ColorByPreferenceColumn}" 
                                          >
    <chartingToolkit:ColumnSeries.DependentRangeAxis>
        <chartingToolkit:LinearAxis Orientation="Y" Minimum="0" Maximum="20" Title="" Foreground="Transparent" ShowGridLines="True">
            <chartingToolkit:LinearAxis.MajorTickMarkStyle>
                <Style TargetType="Line">
                    <Setter Property="Visibility" Value="Collapsed" />
                </Style>
            </chartingToolkit:LinearAxis.MajorTickMarkStyle>
        </chartingToolkit:LinearAxis>
    </chartingToolkit:ColumnSeries.DependentRangeAxis>
</chartingToolkit:ColumnSeries>
<chartingToolkit:Chart.LegendStyle>
    <Style TargetType="Control">
        <Setter Property="Width" Value="0" />
        <Setter Property="Height" Value="0" />
    </Style>
</chartingToolkit:Chart.LegendStyle>
<chartingToolkit:Chart.TitleStyle>
    <Style TargetType="Control">
        <Setter Property="Width" Value="0" />
        <Setter Property="Height" Value="0" />
    </Style>
</chartingToolkit:Chart.TitleStyle>

所以我的问题是:我怎样才能避免所有这些快速的鼠标输入?我想避免我的mouseEnter函数调用"Show()"方法,如果光标没有在超链接上停留至少0.4秒。

我怎么能那样做呢?

谢谢

模拟一个长鼠标在wpf上

您可以使用DispatcherTimer来添加调用函数的延迟,并将其与bool标志相结合,该标志在MouseEnter处理程序中设置为true,在MouseLeave处理程序中设置为false。试试这个:

private DispatcherTimer timer = new DispatcherTimer();
private isMouseOver = false;

timer.Interval = TimeSpan.FromMilliseconds(400);
timer.Tick += Timer_Tick;

private void MouseEnterHandler(object sender, MouseEventArgs e)
{
    isMouseOver = true;
    timer.Start();
}
private void MouseLeaveHandler(object sender, MouseEventArgs e)
{
    isMouseOver = false;
    timer.Stop();
}
private void Timer_Tick(object sender, EventArgs e)
{
    if (isMouseOver) Show();
    timer.Stop();
}