Steveluo’s Blog

Just another WordPress.com weblog

Archive for March, 2009

WPF basics

Posted by steveluo on March 15, 2009

Dependency Property (Notes from WPF unleashed  p45-57)

dependency properties add value on top of plain .NET properties:
. Change notification  — Whenever the value of a dependency property changes, WPF can automatically
                                                 trigger a number of actions depending on the property’s metadata, see trigger
. Property value inheritance  — flowing of property values down the element tree.
. Support for multiple providers  — a five-step process  (see p57)

    want the text in each Button from the About dialog in  to turn blue when the mouse pointer hovers over it.
normal way with WPF trigger
<Button MouseEnter=”Button_MouseEnter” MouseLeave=”Button_MouseLeave”
             MinWidth=”75” Margin=”10”>Help</Button>
<Button MouseEnter=”Button_MouseEnter” MouseLeave=”Button_MouseLeave”
              MinWidth=”75” Margin=”10”>OK</Button>

void Button_MouseEnter(object sender, MouseEventArgs e)
{
        Button b = sender as Button;
        if (b != null)
              b.Foreground = Brushes.Blue;
}
// Restore the foreground to black when the mouse exits the button
void Button_MouseLeave(object sender, MouseEventArgs e)
{
       Button b = sender as Button;
       if (b != null)
              b.Foreground = Brushes.Black;
}
<Trigger Property=”IsMouseOver” Value=”True”>
      <Setter Property=”Foreground” Value=”Blue”/>
</Trigger>

For example, the data binding system in WPF is rooted in the FrameworkElement.DataContext dependency property.  The value of that property is inherited by all descendant elements. For more see Josh’s blog

Business objects — without  dependency properties  (see the details )  
For a business object being bound, there are certain requirements for automatic change notification. There are three approaches you can take:

  • Implement dependency properties in your business object. 
  • Raise an event named <Property Name>Changed. This event must match the property name, and the class must raise the event.
  • Implement the INotifyPropertyChanged in the System.ComponentModel namespace. This interface defines a PropertyChanged event. The business object must fire this event every time any property value changes.

Posted in Uncategorized | Leave a Comment »

SAPI

Posted by steveluo on March 14, 2009

I ran into speech demo Programming Speech in WPFSpeech Synthesis  which is based on Speech Application Programming Interface or SAPI  SDK  5.3  and found sound quality is not good because Xp default voice engine is  
Mircrosoft  Sam (you can find what voice engines have been installed on the computer by following Control Panel->Speech->Speech Properties->Text To Speech tab->Voice selection drop down window)
Try to download Microsoft Anna for my XP.

from
SpeechSynthesizer ttsSynth = new SpeechSynthesizer();
 ttsSynth.SelectVoice(“Microsoft Anna”);
//ttsSynth.Volume =  //ttsSynth.Rate 
 ttsSynth.Speak(“Greetings, my name is ” + ttsSynth.Voice.Name);

Posted in Uncategorized | Leave a Comment »

WPF Resources — open source

Posted by steveluo on March 13, 2009

2D/3D Chart :
  visifire

Posted in WPF | Leave a Comment »

Back to Basics (3)

Posted by steveluo on March 8, 2009

Event
An event is a message sent by an object to signal the occurrence of an action (such as mouse click, or some program logic).
event sender:      The object that raises the event
event receiver:   The object that captures the event and responds to it

A delegate is thus equivalent to a type-safe function pointer or a callback

Posted in Uncategorized | Leave a Comment »

Back to Basics (2)

Posted by steveluo on March 8, 2009

Properties
see  MSDN’s Using Properties
“Properties have many uses: they can validate data before allowing a change; they can transparently expose data on a class where that data is actually retrieved from some other source, such as a database; they can take an action when data is changed, such as raising an event, or changing the value of other fields.”

a)  Auto-Implemented Properties (C#3.0 and up) :
      “the compiler creates a private, anonymous backing field can only be accessed through the property’s get and set accessors.
      The compiler creates backing fields for each auto-implemented property. The fields are not accessible directly from source” code.
// Auto-Impl Properties for trivial get and set
    public double TotalPurchases { get; set; }
    public string Name { get; set; }
    public int CustomerID { get; set; }

b)  Implement an Immutable Class That Has Auto-Implemented 
  “with auto-implemented properties, both a get and set accessor are required. You make the class immutable by declaring the set accessors as private. However, when you declare a private set accessor, you cannot use an object initializer to initialize the property. You must use a constructor or a factory method.”

Posted in Uncategorized | Leave a Comment »

MVVM: How to

Posted by steveluo on March 1, 2009

I have learned the following best practices from thess great articles.

Karl Shifflet and Adam Kinney talk about ViewModel on Channel9: http://channel9.msdn.com/shows/Continuum/MVVM/

  • Jeff Handley has a number of great post on ViewModel: http://blog.jeffhandley.com/archive/2008/10/21/viewmodel-pattern.aspx[1]Introduction to Model/View/ViewModel pattern for building WPF apps  by John Grossman
    [2]Patttern MVVM  by Josh Smith
    [3]The ViewModel Pattern by David Hill
    [4]http://www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmv 

    1)  How to lay out Xaml files
        
    2) Relay Command
    3) Treeview’ double click  — 
    4) Datagrid  binding
    5) Flat to tree list
  • Posted in WPF | Tagged: | Leave a Comment »