TabItem悬停在其任何控件的悬停事件中

时间:2013-11-22 20:44:00

标签: wpf events controls tabitem

当我将一个控件(如文本框或按钮)悬停在选项卡控件中时,它会悬停TabItem的标头,实现其IsHover属性样式。 组合框的选择更改事件也会发生这种情况,因为它会触发TabControl的选择更改事件。

我知道我可以将处理的悬停事件处理程序的Eventargs设置为true。

e.Handled=true;

我有许多标签控件,其项目包括数百个控件。 所以我认为在所有标签控件中处理每个控件的悬停和选择更改事件是一个坏主意。 我需要将Tabitem的事件与其内容的事件完全分开。

这就是我的Xaml:

    <TabControl x:Name="tc_main" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Background="Transparent" BorderThickness="0" Margin="10,0,-10,0" SelectionChanged="tc_main_SelectionChanged">

        <TabItem Header="المحددات" x:Name="tci_id" Style="{DynamicResource TabItemStyle1}"  Background="#FF892C7F" >
                        <Grid Width="1339.55" Style="{DynamicResource GridStyle1}">
                       <Button Content="Button" Margin="242,194,851.55,388" Click="Button_Click_1"/>



                            <TextBox Margin="234,103,859.55,509" TextWrapping="Wrap" Text="TextBox" FontSize="24" />
                            <Button Content="Button" Margin="590,194,503.55,388" Click="Button_Click_1"/>
                            <TextBox Margin="590,103,516,509" TextWrapping="Wrap" Text="TextBox" Width="233.55"/>
                            <Button Content="Button" Margin="234,474,859.55,108" Click="Button_Click_1"/>
                            <TextBox Margin="234,383,872,229" TextWrapping="Wrap" Text="TextBox" Width="233.55"/>
                            <Button Content="Button" Margin="590,474,503.55,108" Click="Button_Click_1"/>
                            <TextBox Margin="590,383,516,229" TextWrapping="Wrap" Text="TextBox" Width="233.55"/>
                            <Label x:Name="ss" Content="Label" HorizontalAlignment="Left" Height="30" Margin="112.775,103,0,0" VerticalAlignment="Top" Width="98"/>
                            <ComboBox HorizontalAlignment="Left" Height="30" Margin="234,44,0,0" VerticalAlignment="Top" Width="246" IsEditable="True" Background="{x:Null}" >
                                <ComboBoxItem Content="ti1" >

                                </ComboBoxItem>
                                <ComboBoxItem Content="ti2" >

                                </ComboBoxItem>
                                <ComboBoxItem Content="ti3" >

                                </ComboBoxItem>

                            </ComboBox>
                            <Button Content="Button" Margin="495,103,762.05,450.5" Click="Button_Click_2"/>
                       </Grid>
        </TabItem>
</TabControl>

一旦我更改了组合框的选择,就会触发以下事件。

 private void tc_main_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tc_main.SelectedItem == tci_id)
            {
                // Do something
            }
        }

当我悬停任何控件时,也触发TabItem的以下样式。 这是tabitem悬停的样式

<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
    <Style.Triggers>
        <Trigger Property="Selector.IsSelected" Value="True">
        <Setter Property="FontSize" Value="27"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="RenderTransform">
            <Setter.Value>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform Y="-5"/>
                </TransformGroup>
            </Setter.Value>
        </Setter>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
    <Setter Property="Foreground" Value="#FFE8E8E8"/>
    <Setter Property="FontSize" Value="24"/>
    <Setter Property="Background" Value="{StaticResource TabItem.Static.Background}"/>
    <Setter Property="BorderBrush" Value="{StaticResource TabItem.Static.Border}"/>
    <Setter Property="Padding" Value="25,13,18,6"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="MinHeight" Value="50"/>
    <Setter Property="Margin" Value="8,0,8,0"/>
    <Setter Property="Header" Value="عنصر"/>
    <Setter Property="FontFamily" Value="Adobe Arabic"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                    <Border x:Name="mainBorder"
                     BorderBrush="{TemplateBinding BorderBrush}" 
                     BorderThickness="0,0,0,0" 
                     Background="{TemplateBinding Background}" 
                     Margin="0">
                    <Border x:Name="innerBorder" 
                    BorderBrush="{StaticResource TabItem.Selected.Border}" 
                    BorderThickness="0,0,0,0" 
                    Opacity="0"/>
                    </Border>
                    <ContentPresenter x:Name="contentPresenter" ContentSource="Header" Focusable="False" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                            <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Left"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Border}"/>
                                                <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,0,0,0"/>
                        <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,0,0,0"/>
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                            <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Bottom"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Border}"/>
                                                <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,0,0,0"/>
                        <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,0,0,0"/>
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                            <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Right"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Border}"/>
                                                <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,0,0,0"/>
                        <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,0,0,0"/>
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
                            <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Top"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Background}"/>
                        <Setter Property="BorderBrush" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Border}"/>
                        <Setter Property="BorderThickness" TargetName="innerBorder" Value="0,0,0,0"/>
                        <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,0,0,0"/>
                    </MultiDataTrigger>

                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="true"/>
                            <Condition Binding="{Binding TabStripPlacement, RelativeSource={RelativeSource AncestorType={x:Type TabControl}}}" Value="Top"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="BorderBrush" TargetName="mainBorder" Value="{StaticResource TabItem.MouseOver.Border}"/>
                        <Setter Property="BorderThickness" TargetName="mainBorder" Value="0,0,0,7"/>
                    </MultiDataTrigger>


                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    <Setter Property="RenderTransform">
        <Setter.Value>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform Y="-7"/>
            </TransformGroup>
        </Setter.Value>
    </Setter>
</Style>

0 个答案:

没有答案