10

私がこのコードを持っているとしましょう:

<Window>
    <Window.Resources>
        <Color x:Key="MyColor"
               A="255"
               R="152"
               G="152"
               B="152" />
        <DropShadowEffect x:Key="MyEffect" 
                          ShadowDepth="0"
                          Color="{StaticResource MyColor}"
                          BlurRadius="10" />
        <Style x:Key="MyGridStyle"
               TargetType="{x:Type Grid}">
            <Setter Property="Height"
                    Value="200" />
            <Setter Property="Width"
                    Value="200" />
            <Style.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Width"
                            Value="100" />
                </Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Height"
                            Value="100" />
                    <Setter Property="Width"
                            Value="100" />
                </Style>
            </Style.Resources>
            <Style.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="true">
                    <!-- How do I apply my effect when this grid is hovered over to Image and TextBox, but not the grid itself? -->
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid Style="{StaticResource MyGridStyle}">
        <Grid.RowDefinitions>
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Image Grid.Row="0"
               Grid.Column="0"
               Source="image.png" />
        <TextBlock Grid.Row="0"
                   Grid.Column="0"
                   Text="Hover Over Me" />
    </Grid>
</Window>

基本的に、グリッドに適用されたスタイルがあり、その中のTextBlockまたはImageは特定のサイズのスタイルである必要があります。

グリッド上にトリガーを作成して、グリッド内のすべてのTextBlockと画像にエフェクトを適用しますが、グリッド自体には適用しないようにします。

トリガーをTextBlockやImageに直接適用できますが、その場合、効果は各要素で個別にのみ発生します。カーソルを合わせた内側の子要素に関係なく、グリッド内のすべてのTextBlockやImageに効果を発生させる必要があります。

誰かがこれを手伝ってくれますか?

4

2 に答える 2

22

あなたはそれを逆にすることができます。つまり、を追加DataTriggersImageて、祖先に対してTextBlockトリガーをオンにします。IsMouseOverGrid

注:マウスが上にくるとすぐにこの効果をトリガーする場合は、のような値Gridを設定する必要があります。デフォルトでは、isとこの値はヒットテストでは使用されません。BackgroundTransparentBackgroundnull

<Style x:Key="MyGridStyle" TargetType="{x:Type Grid}">
    <!--<Setter Property="Background" Value="Transparent"/>-->
    <Setter Property="Height" Value="200" />
    <Setter Property="Width" Value="200" />
    <Style.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Width" Value="200" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Grid},
                                               Path=IsMouseOver}" Value="True">
                    <Setter Property="Effect" Value="{StaticResource MyEffect}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Height" Value="200" />
            <Setter Property="Width" Value="200" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Grid},
                                               Path=IsMouseOver}" Value="True">
                    <Setter Property="Effect" Value="{StaticResource MyEffect}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Style.Resources>
</Style>
于 2011-10-05T20:19:31.747 に答える
1

以前は、行全体ではなく、リストボックスの行のコンテンツのみを外側に光らせるという同様の要件がありました。この記事を参考にしました... http://drwpf.com/blog/2008/03/25/itemscontrol-i-is-for-item-container

于 2011-10-05T20:07:46.700 に答える