禁用元素上的事件

时间:2013-07-25 12:01:12

标签: silverlight windows-phone-7 xaml windows-phone-8

我遇到了问题,放在其他元素中的一些元素搞乱了事件,例如:

我有扩展器的代码:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Expander">
<Style TargetType="local:Expander">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:Expander">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="ViewStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.0"/>
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Expanded">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="ContentScaleTransform"
                                    Storyboard.TargetProperty="ScaleY" To="1" Duration="0"/>

                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Collapsed">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="ContentScaleTransform"
                                    Storyboard.TargetProperty="ScaleY" To="0" Duration="0"/>

                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border BorderBrush="#FFCAADAD"
                    BorderThickness="1"
                    CornerRadius="{TemplateBinding CornerRadius}"
                    Background="{TemplateBinding Background}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Grid  Background="#FFDBDBDB" Margin="0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="80"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Grid.Column="0" HorizontalAlignment="Center" Grid.ColumnSpan="2" VerticalAlignment="Center" Content="{TemplateBinding HeaderContent}" Canvas.ZIndex="1"/>
                                <ToggleButton Grid.Column="0" Margin="10,0,0,0" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" RenderTransformOrigin="0.5,0.5" x:Name="ExpandCollapseButton">
                                    <ToggleButton.Template>
                                        <ControlTemplate>
                                            <Grid>
                                                <VisualStateManager.VisualStateGroups>
                                                    <VisualStateGroup x:Name="ViewStates">
                                                        <VisualStateGroup.Transitions>
                                                            <VisualTransition GeneratedDuration="0:0:0.0"/>
                                                        </VisualStateGroup.Transitions>
                                                        <VisualState x:Name="Checked">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetName="RotateButtonTransform" Storyboard.TargetProperty="Angle" To="180" Duration="0"/>
                                                            </Storyboard>
                                                        </VisualState>
                                                        <VisualState x:Name="Unchecked">
                                                            <Storyboard>
                                                                <DoubleAnimation Storyboard.TargetName="RotateButtonTransform" Storyboard.TargetProperty="Angle" To="0" Duration="0"/>
                                                            </Storyboard>
                                                        </VisualState>
                                                    </VisualStateGroup>
                                                </VisualStateManager.VisualStateGroups>
                                                <Rectangle HorizontalAlignment="Stretch" Height="80" Fill="#FFDBDBDB"/>
                                                <Path RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" VerticalAlignment="Center" Data="M2,3L9,10 16,3" Stroke="Black" StrokeThickness="6" Fill="#FFDBDBDB">
                                                    <Path.RenderTransform>
                                                        <RotateTransform x:Name="RotateButtonTransform"/>
                                                    </Path.RenderTransform>
                                                </Path>
                                            </Grid>
                                        </ControlTemplate>
                                    </ToggleButton.Template>

                                </ToggleButton>
                            </Grid>
                            <ContentPresenter Grid.Row="1" Margin="0" Content="{TemplateBinding Content}" x:Name="Content">
                                <ContentPresenter.RenderTransform>
                                    <ScaleTransform x:Name="ContentScaleTransform"/>
                                </ContentPresenter.RenderTransform>
                            </ContentPresenter>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

问题在于:

<ContentPresenter Grid.Column="0" HorizontalAlignment="Center" Grid.ColumnSpan="2" VerticalAlignment="Center" Content="{TemplateBinding HeaderContent}" Canvas.ZIndex="1"/>

它位于我的Toggle按钮之上,因此文本可见,问题是:

当我点击扩展器时如果我点击文本的地方 - 扩展器dos不扩展,如果我不接触文本,但只是空字段它会扩展。这意味着 Text_Tap事件正在被触发。

有没有办法禁用它?

我试图将Contentpresenter放入按钮,但是它没有看到biding - 我不知道如何解决这个问题,也许还有其他选择?

1 个答案:

答案 0 :(得分:0)

设置文字属性IsHitTestVisible = false