none
Global dependency?

    Question

  •  

    Hi

     

    Is there a way to use global dependency on triggers? My problem is, that I have made a button custom control wich have some trigger animations on "MouseOver", wich set a border around the button. This part works perfect, but I want to be able to turn this animation effects off from a global variable, so this variable has a influence on all buttons on the page once for all.

     

    Something like:

     

    <MultiTrigger>
        <
    MultiTrigger.Conditions>
              <
    Condition Property="MyApplication.IsMouseOn" Value="True"/>
              <
    Condition Property="IsMouseOver" Value="True"/>
        </
    MultiTrigger.Conditions>

       <MultiTrigger.EnterActions>

       .......

       </MultiTrigger.EnterActions>

    </MultiTrigger>

    Is this possible?
    Wednesday, April 23, 2008 1:10 PM

Answers

  • Condition has a property called SourceName that would allow you to use something like your Window instance as the source of the property. Then, if you really want to control it at the application level, you can using a Binding to bind the Window instance's property to that application level property (such as a static or a resource). You could even define the property as an attached property so that you don't need to use a custom Window class.

     

    HTH,
    Drew

    Wednesday, April 23, 2008 6:39 PM
    Moderator
  • Drew's suggestion should be able to do the trick, or you could try using DataTrigger instead, something like the following should work:

    Code Snippet

    <DataTrigger Binding="{Binding Path=IsMouseOn, Source={x:Static Application.Current}}" Value="True">
       <Setter TargetName="MyBorder" Property="IsEnabled" Value="False"/>
    </DataTrigger>


    Hope this helps


    Friday, April 25, 2008 5:02 AM
  • You could have the App class implement INotifyPropertyChanged interface, or define the "IsMouseOn" property as a dependency property.

    Hope this helps
    Wednesday, April 30, 2008 3:02 AM

All replies

  • Condition has a property called SourceName that would allow you to use something like your Window instance as the source of the property. Then, if you really want to control it at the application level, you can using a Binding to bind the Window instance's property to that application level property (such as a static or a resource). You could even define the property as an attached property so that you don't need to use a custom Window class.

     

    HTH,
    Drew

    Wednesday, April 23, 2008 6:39 PM
    Moderator
  • Thanks for your response Drew Marsh.

     

    I'm not sure that I know what you mean, but I've tried this with no luck.

     

    --MyApplication--

    public static DependencyProperty MouseOnProperty = DependencyProperty.Register("IsMouseON", typeof(bool), typeof(MyApplication), new PropertyMetadata(false));

    [Category("Appearance"), Description("Is the mouse on")]

    public bool IsMouseON

    {

    get { return (bool)base.GetValue(MouseOnProperty); }

    set { base.SetValue(MouseOnProperty, value); }

    }

     

     

    --Page 2---

    <a:MyButton.Triggers>

    <Trigger SourceName="MyApplication" Property="IsMouseON" Value="False">

           <Setter TargetName="MyBorder" Property="IsEnabled" Value="False"/>

    </Trigger>

    </a:MyButton.Triggers>

     

     

    I gets the following compiler error:

     

    Error 9 'IsMouseON' member is not valid because it does not have a qualifying type name. D:\Test\Page2.xaml 1 81 TestApp

     

    Am I missing something?

    Spandy
    Wednesday, April 23, 2008 10:04 PM
  • Drew's suggestion should be able to do the trick, or you could try using DataTrigger instead, something like the following should work:

    Code Snippet

    <DataTrigger Binding="{Binding Path=IsMouseOn, Source={x:Static Application.Current}}" Value="True">
       <Setter TargetName="MyBorder" Property="IsEnabled" Value="False"/>
    </DataTrigger>


    Hope this helps


    Friday, April 25, 2008 5:02 AM
  • Thanks Marco Zhou

     

    But how can I make a DependencyProperty inside the current application, when GetValue/SetValue doesn't exists?

     

    public partial class App : Application

    {

    public static DependencyProperty IsMouseOnProperty =

    DependencyProperty.Register("IsMouseOn", typeof(bool),

    typeof(App));

     

    public bool IsMouseOn

    {

    get { return (bool)GetValue(IsMouseOnProperty); }

    set { SetValue(IsMouseOnProperty, value); }

    }

    ....

    }
    Monday, April 28, 2008 12:36 PM
  • You cannot define Dependency Properties on a class which doesn't derive from DependencyObject, but you could define attached Dependency Property on them as Drew suggested above, here is an example:

    public partial class App : Application
    {
        public static Boolean GetIsMouseOn(DependencyObject obj)
        {
            return (Boolean)obj.GetValue(IsMouseOnProperty);
        }

        public static void SetIsMouseOn(DependencyObject obj, Boolean value)
        {
            obj.SetValue(IsMouseOnProperty, value);
        }

        public static readonly DependencyProperty IsMouseOnProperty = DependencyProperty.RegisterAttached(
            "IsMouseOn",
            typeof(Boolean),
            typeof(App), new
                UIPropertyMetadata(false));
    }
    Or you could use the data trigger method I suggested above which is probably much simpler than Drew's

    Hope this helps
    Tuesday, April 29, 2008 6:31 AM
  • Thanks a lot Marco Zhou

     

    I tried your data trigger method, and it worked perfect, but when I try to change the "IsMouseOn" property on the app in runtime, then nothing happens to the UI.

     

    How do you setup the "IsMouseOn" property inside the App class, so the data trigger event is fired when the IsMouseOn value is changed?

     

    Spandy

     

    Tuesday, April 29, 2008 11:56 AM
  • You could have the App class implement INotifyPropertyChanged interface, or define the "IsMouseOn" property as a dependency property.

    Hope this helps
    Wednesday, April 30, 2008 3:02 AM
  • Thanks Marco Zhou

     

    Wednesday, April 30, 2008 7:18 AM