WP7 Itemtemplate单​​击事件在折叠的ExpanderView上触发

时间:2012-05-29 15:32:53

标签: windows-phone-7 silverlight-toolkit

我使用Silverlight Toolkit中提供的ExpanderView控件和一些自定义模板。这一切都运行良好,但是当ExpanderView折叠时,我点击扩展ExpanderView时项目所在的Header下面的区域。触发该项目的点击事件。

我该如何解决这个问题?我应该以什么方式删除tap命令或在ExpanderView折叠时删除ItemPanel并在扩展时再次添加它?

<DataTemplate x:Key="CustomItemTemplate">
        <Image delay:LowProfileImageLoader.UriSource="{Binding}" Width="156" Height="95" >
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Tap">
                    <cmd:EventToCommand Command="{Binding Storage.ImageTapCommand, Source={StaticResource Locator}}" CommandParameter="{Binding}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Image>
</DataTemplate>

<toolkit:ExpanderView Grid.Column="1" Header="{Binding}"
                  Expander="{Binding}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}"
                  ItemsSource="{Binding Files}" HeaderTemplate="{StaticResource CustomHeaderTemplate}"
                  ExpanderTemplate="{StaticResource CustomExpanderTemplate}"
                  ItemTemplate="{StaticResource CustomItemTemplate}" >
      <toolkit:ExpanderView.ItemsPanel>
             <ItemsPanelTemplate>
                    <toolkit:WrapPanel />
              </ItemsPanelTemplate>
      </toolkit:ExpanderView.ItemsPanel>
</toolkit:ExpanderView>

1 个答案:

答案 0 :(得分:2)

每次扩展/折叠ExpanderView时,您都可以为每个扩展器项更改根UIElement的IsHitTestVisible属性,也可以在最初绑定ExpanderView之后(连接到ExpanderView.LayoutUpdated可以正常工作)。这是一个为我解决问题的例子:

    private void FixExpanderItemsInteractivity(ExpanderView expanderView)
    {
        foreach (var item in expanderView.Items)
        {
            ContentPresenter contentPresenter = expanderView.ItemContainerGenerator.ContainerFromItem(item) as ContentPresenter;

            if (contentPresenter != null)
            {
                UIElement expanderItemRootElement = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
                if(expanderItemRootElement != null)
                {
                    expanderItemRootElement.IsHitTestVisible = expanderView.IsExpanded;
                }
            }
        }
    }

    private void Expander_Expanded(object sender, RoutedEventArgs e)
    {
        FixExpanderItemsInteractivity(sender as ExpanderView);
    }

    private void Expander_Collapsed(object sender, RoutedEventArgs e)
    {
        FixExpanderItemsInteractivity(sender as ExpanderView);
    }

    private void Expander_LayoutUpdated(object sender, EventArgs e)
    {
        FixExpanderItemsInteractivity(sender as ExpanderView);
    }