Wednesday, January 30, 2008

3D in .NET - Business & leisure!

This is a Dotway seminar presented in Malmö, Göteborg, Stockholm and Linköping. Visit www.dotway.se for more information. It's free!

The world sees an ever increasing amount of data. Visualizing and utilizing the data is one of the largest technical challenges for todays developers and designers. With Net 3.0 and WPF, Microsoft brings the third dimension closer than ever and 3D graphics is no longer the exclusive domain of game developers. We show how you could leverage the 3D-support of WPF in every day business solutions.
Furthermore we explain why XNA is most suited for gaming solutions, why Silverlight is not 3D-ready yet and why these things should matter to you.

http://www.dotway.se/Default.aspx?tabid=112

Monday, January 21, 2008

Reset a DependencyProperty to its default value

If we have a dependency property:

public static readonly DependencyProperty MyExampleValueProperty = DependencyProperty.Register("MyExampleValue", typeof(double), typeof(MyClass), new PropertyMetadata(3.1592));

public double MyExampleValue
{
   
set { SetValue(MyExampleValuePropertyProperty, value); }
   
get { return (double)GetValue(MyExampleValuePropertyProperty); }
}

After the value has been changed we might reach a point where we want to reset this property to its default value (3.1592). This can be done in two ways:

  • MyExampleValue = (double)MyClass.MyExampleValueProperty.DefaultMetadata.DefaultValue;
  • this.ClearValue(MyClass.MyExampleValueProperty);

But there are some things to note here: If we have a scenario where we want to inherit the value if the dependency property is not set in the current class, then we can not use the first example (MyExampleValue =...). Why? Because we are setting the current instance to a value, so the value will not be inherited (the only thing we are doing is setting the value to the same as the default for the property). In the second example this will work since we are clearing the property instead of setting it.

Wednesday, January 16, 2008

Custom Attributes in .NET

I found a very good blog post about Attributes in .NET. This is a pretty important functionality in all the .NET languages and can be very useful in a lot of situations. Event if custom attributes have great powers and enables a lot of thing they are not used very often (not in examples at least). So if you want an introduction to the subject, look here: http://blogs.msdn.com/gordenlin/archive/2008/01/16/net-custom-attribute-learning-and-practice.aspx.

Monday, January 14, 2008

Trigger – EnterActions and ExitActions vs. EventTrigger – Actions

  • What’s the difference between Trigger and EventTrigger?
  • What are EnterActions and ExitActions and how do they compare to Actions?

Triggers
Triggers corresponds to the change of a property such as IsMouseOver, IsPressed (Button), IsSelected (ListBox).

Example:
<
Trigger Property="IsMouseOver" Value="True">
   
<Trigger.Setters>
        <
Setter Property="Foreground" Value="Red" />
   
</Trigger.Setters>
</Trigger>
This trigger will be activated when the IsMouseOver-property becomes true.

EventTriggers
EventTriggers is activated when an event (RoutedEvent) is raised. When the specified routed event is raised a set of Actions are activated.

Example:
<EventTrigger RoutedEvent="Click">
   
<EventTrigger.Actions>
       
<BeginStoryboard>
           
<BeginStoryboard.Storyboard>
               
<Storyboard>
                   
<DoubleAnimation Storyboard.TargetProperty="Width" Duration="0:0:1" From="100" To="400" AutoReverse="true" />
               
</Storyboard>
           
</BeginStoryboard.Storyboard>
       
</BeginStoryboard>
   
</EventTrigger.Actions>
</EventTrigger>
The EventTrigger above will be raised when the button is pressed so that the Click event is raised.

Conclusion: Trigger is something that is activated from property changes and EventTrigger is something that is activated when a RoutedEvent is raised.

 

That’s the basic of Triggers and EventTriggers. But if we look more closely at Trigger we see that it also contains EnterActions and ExitActions. The Trigger example above could be extended to the following:
<Trigger Property="IsMouseOver" Value="True">
   
<!-- Add a trigger when the mouse is entering the button. -->
   
<Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
               
<DoubleAnimation Storyboard.TargetProperty="Opacity" To="1.0" Duration="0:0:1" />
            </Storyboard>
        </
BeginStoryboard>
   
</Trigger.EnterActions>

   
<!-- Add a trigger when the mouse is leaving the button. -->
    <Trigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"              
                To
="0.2" Duration="0:0:1" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.ExitActions>
    <Trigger.Setters>
        <Setter Property="Foreground" Value="Red" />
    </Trigger.Setters>   
</Trigger>


The condition is that the Trigger should be activated when the IsMouseOver-property is set to true. When this happens the EnterAction will apply and also all the Setters in Trigger.Setters. ExitActions will only be activated when the property is set to false again. The difference between EnterActions/ExitActions and the Setters is the following; If we use Setters the value we set will automatically go back to the value it had before the Trigger kicked in. When we use EnterActions or ExitActions these values will be set permanently.
To be more explicit; If we remove the ExitActions in the code above we will only have EnterActions that will set Opacity to 1.0 (default was set to 0.5 in my code) and also the Setter that will change the Foreground to Red when the mouse is over the button. If we move the mouse away from the button the Foreground will go back to its default value (probably black) but the Opacity will still be 1.0.

The conclusion is that EnterActions and ExitActions we set values permanently and Setters will only set temporary values that will be applied as long as the property condition is fulfilled in the Trigger. We can also note that EventTriggers also have EnterActions and ExitActions but these can not be set (we will get a runtime error).

Also download the example project and source here.