2011-10-05 13 views
8

चलें कहते हैं कि मैं कोड के इस बिट है:बाल के लिए WPF माउस ले जाएँ उत्प्रेरक प्रभाव को नियंत्रित करता है

<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 या छवि एक निश्चित आकार के लिए शैलियों होना चाहिए करने के लिए लागू ।

मैं ग्रिड पर एक ट्रिगर बनाना चाहता हूं जो ग्रिड के भीतर सभी टेक्स्टब्लॉक और छवियों पर प्रभाव लागू करने का कारण बनता है, लेकिन ग्रिड के लिए नहीं।

मैं सीधे ट्रिगर को टेक्स्टब्लॉक और/या छवि पर लागू कर सकता हूं, लेकिन तब प्रभाव केवल प्रत्येक तत्व पर अलग-अलग होता है। मुझे ग्रिड के भीतर किसी भी टेक्स्टब्लॉक और/या छवि के प्रभाव होने की आवश्यकता है, इसके बावजूद कि अंदरूनी बाल तत्व मैं किस पर आच्छादित हूं।

क्या कोई इस से मेरी सहायता कर सकता है?

उत्तर

20

आप इसे दूसरी तरफ कर सकते हैं। यही है, DataTriggersImage और TextBlock जोड़ें और पूर्वजों Grid के लिए उन्हें IsMouseOver पर ट्रिगर करें।

नोट: आप इस प्रभाव को जैसे ही माउस खत्म हो गया है को गति प्रदान करना चाहते हैं Grid आप एक मूल्य के लिए Background स्थापित करने के लिए, Transparent तरह की आवश्यकता होगी। डिफ़ॉल्ट रूप से, Backgroundnull है और यह मान हिट परीक्षण में उपयोग नहीं किया जाता है।

<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> 
1

हमें एक बार एक सूची बॉक्स की पंक्ति की सामग्री केवल बाहरी चमक की एक समान आवश्यकता थी, कुल मिलाकर पंक्ति नहीं। हमने इस लेख की मदद ली ... http://drwpf.com/blog/2008/03/25/itemscontrol-i-is-for-item-container

संबंधित मुद्दे