c# - 如何检测点击当前选定的 PivotItem 的标题

标签 c# xaml events windows-phone-8 windows-phone

我有一个 Pivot 控件。我想要一个特殊的行为——每当用户点击当前选择的 PivotItem 的标题时,我都想对此使用react。但是,当点击的标题不属于当前选定的数据透视项时,不会发生此 react 。

我的计划如下:

为每个 PivotItem 创建一个自定义 header 并将其点击事件与处理程序相关联:

<phone:PivotItem DataContext="{Binding}" ContentTemplate="{StaticResource MyTemplate}" Content="{Binding}" x:Name="itemA">
    <phone:PivotItem.Header>
        <TextBlock x:Name="headerA" Text="A" Tap = "HeaderA_Tapped"/>
    </phone:PivotItem.Header>
</phone:PivotItem>

然后在处理程序中,测试当前是否选择了点击的项目,如果是,则使用react:

protected void HeaderA_Tapped(object sender, GestureEventArgs e)
{
    if (mainPivot.SelectedItem.Equals(itemA))
    {
        //selected item is the same pivotItem that reported tapping event
        react();
    }
}

这看起来很简单,但在尝试之后我发现点击事件仅在选择更改事件之后报告。在用户点击当前未选择的 pivotItem 标题的情况下,pivot 将相应地更改选择(我想保留的默认行为),然后才报告点击事件。但是,这对我的代码来说为时已晚,因为在那一刻点击的标题和当前选择的项目已经相同。

有什么方法可以检测点击是否启动了选择更改?或者有没有办法恢复事件的顺序?我猜目前 WP 事件模型将事件从 Visual Tree 的根下沉到叶子 -> 因此 Pivot 更快地处理它,然后它才到达标题 TextBlock.

最佳答案

我认为您应该使用计时器跟踪 Pivot SelectedIndex 并在 Selection_Changed 事件中更新 SelectedIndex。 有些人是这样认为的:

int selectedIndex;
func Selection_ChangedEvent()
{
    //use a timer to update selectedIndex
    //something like: Timer.do(selectedIndex = Pivot.SelectedIndex, 100)
    //fire after a specify time. adjust this for the best result. 
}

在你点击的事件中

HeaderA_Tapped()
{
    if(selectedIndex == "index of item A in Pivot")
    {
        react();
    }
}

关于c# - 如何检测点击当前选定的 PivotItem 的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219030/

相关文章:

c# - XNA 旋转纹理 2D

c# - 如何为派生类实现 INotifyPropertyChanged?

javascript - 如何使用 jQuery 知道 "select"输入值已更改?

events - 事件触发时获取 ID 属性 (elm v0.19.1)

c# - ASP MVC 文件上传 HttpPostedFileBase 为空

c# - ReSharper“无法解析符号”,即使在项目构建时

c# - XAML控件使用“可见性”类型而不是普通“ bool ”的实际原因是什么?

具有自定义事件参数的 C# 反射程序化事件处理程序

c# - 无法在网格单元格内找到控件

c# - 如何在 Windows 10 上的 UWP 中输出到控制台?