Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Using C# dependency property in XAML Triggers

Using C# dependency property in XAML Triggers

Scheduled Pinned Locked Moved WPF
csharpcssdatabasewpfquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Prasoon Chaudhary
    wrote on last edited by
    #1

    While creating style for a button I am writing like this:

    <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Button}">
      <Grid>
                <Image Name="Normal" Source="Resources/Images/n0.png"/>
    	<Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/>
                <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/>
                <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/>
          </Grid>
         <ControlTemplate.Triggers>
           <Trigger Property="IsMouseOver" Value="True">
               <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
               <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/>
           </Trigger> 
    

    Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:

    Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?

    G P 3 Replies Last reply
    0
    • P Prasoon Chaudhary

      While creating style for a button I am writing like this:

      <Setter Property="Template">
      <Setter.Value>
       <ControlTemplate TargetType="{x:Type Button}">
        <Grid>
                  <Image Name="Normal" Source="Resources/Images/n0.png"/>
      	<Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/>
                  <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/>
                  <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/>
            </Grid>
           <ControlTemplate.Triggers>
             <Trigger Property="IsMouseOver" Value="True">
                 <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                 <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/>
             </Trigger> 
      

      Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:

      Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?

      G Offline
      G Offline
      George Nistor
      wrote on last edited by
      #2

      I have the same problem: I need to put lines with different colors in a datagrid base on what happens in code behind. I think one solution will be to attach a style with a trigger from the code behind. Of course I wait to see how can access a code declared dependency property from xaml. Maybe with a DataTrigger.

      1 Reply Last reply
      0
      • P Prasoon Chaudhary

        While creating style for a button I am writing like this:

        <Setter Property="Template">
        <Setter.Value>
         <ControlTemplate TargetType="{x:Type Button}">
          <Grid>
                    <Image Name="Normal" Source="Resources/Images/n0.png"/>
        	<Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/>
                    <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/>
                    <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/>
              </Grid>
             <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="True">
                   <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                   <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/>
               </Trigger> 
        

        Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:

        Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        I wouldn't approach it like this. A simpler way to do this would be to implement a value converter which did the heavy work of just displaying the appropriate image, rather than showing and hiding elements.

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        P 1 Reply Last reply
        0
        • P Prasoon Chaudhary

          While creating style for a button I am writing like this:

          <Setter Property="Template">
          <Setter.Value>
           <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                      <Image Name="Normal" Source="Resources/Images/n0.png"/>
          	<Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/>
                      <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/>
                      <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/>
                </Grid>
               <ControlTemplate.Triggers>
                 <Trigger Property="IsMouseOver" Value="True">
                     <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
                     <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/>
                 </Trigger> 
          

          Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:

          Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?

          G Offline
          G Offline
          George Nistor
          wrote on last edited by
          #4

          I have solved my problem like here: http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx http://msdn.microsoft.com/en-us/library/ms668604.aspx and I can put different colors in my grid based on the normal property from the Item class.

          P 1 Reply Last reply
          0
          • P Pete OHanlon

            I wouldn't approach it like this. A simpler way to do this would be to implement a value converter which did the heavy work of just displaying the appropriate image, rather than showing and hiding elements.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            P Offline
            P Offline
            Prasoon Chaudhary
            wrote on last edited by
            #5

            Thanks! Yes your approach is better one. I opted for INotifyPropertyChanged and sorted out my issue. :)

            1 Reply Last reply
            0
            • G George Nistor

              I have solved my problem like here: http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx http://msdn.microsoft.com/en-us/library/ms668604.aspx and I can put different colors in my grid based on the normal property from the Item class.

              P Offline
              P Offline
              Prasoon Chaudhary
              wrote on last edited by
              #6

              Thanks for sharing those links. Meanwhile I sorted out my requirement using 'INotifyPropertyChanged'.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups