venerdì 10 febbraio 2006
Using BitmapEffects

“You can easily apply effects like Blur or DropShadows to a UI element using BitmapEffects property.

This xaml sample:

<Image HorizontalAlignment="Left" VerticalAlignment="Top" Source="cor.jpg">
      <BlurBitmapEffect Radius="2"/>
<TextBox Text="Hello WPF">
      <OuterGlowBitmapEffect GlowColor="Green" GlowSize="5" Noise="3"  />
Produces this effect:

As you can see various effects can be applied to the same element, just keep in mind that effects are processed in sequence so the order of appearance inside counts.”
Unfortunately Sparkle does not support direct editing of this property yet.

posted @ lunedì 1 gennaio 0001 00:00 | Feedback (1126)
Databinding using "Sparkle"

How to create a user interface that displays data from an external DataSource using Expression Interactive Designer (“Sparkle“)

  • Create a new Visual C# Library project named PersonsDataSource
  • Remove Class1 and add both Person and Persons class (described in my previous blog)
  • Compile project, you should now have a PersonsDataSource dll.
  • Open Sparkle and create a new Project (File ->New Project) name it as you like.
  • Now let's add a reference to our PersonsDataSource.dll using Project->Add Item to browse to the previously created assembly.
  • You'll now see the dll listed under Project references.
  • Open the Data Palette (View->Data) and select Add new CLR DataSource, browse to PersonsDataSource and select Persons item, you should now see Persons listed inside Data palette.
  • Select Persons item from Data palette and drag it to Document root surface
  • You'll be asked to select how the Persons datasource will be displayed, select ListBox and click Ok.
  • The Create Data Template appears: This dialog allows you to 'shape' the DataTemplate associated to Person object, you can select what items and what controls to map to various Person fields (in this case dialog defaults to TextBlocks) leave defaults and click Ok.
  • You'll should see the listbox filled with Persons items.
  • We now want to add 2 texboxes that will be populated with Listbox selected item.
  • Select Library palette and drag two textboxes on DocumentRoot surface.
  • Select first texbox and, on Properties palette, click on Text field and select DataBind...
  • You'll now see the Create Data Binding dialog, we need to bind to Listbox selected item so click on Element Property and select ListBox.
  • Select SelectedItem among ListBox properties.
  • Listbox contains instances of Person classes so, select Use custom path expression and type “SelectedItem.Name” (without quotes) on related textbox, then click Ok.
  • Do the same with other textbox using “SelectedItem.Age” as custom path expression.
  • Press F5 and you'll see that selecting a listbox item selection will be reflected on both textboxes and that changes on textbox contents will be moved to underlying DataSource.
posted @ lunedì 1 gennaio 0001 00:00 | Feedback (3605)
A little bit of WPF DataBinding...

Let's assume you wish to use DataBinding to manage a set of Person class instances.

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

 public class Person
  public Person (string name, int age)
   this.Name = name;
   this.Age = age;

  private int mAge;
  public int Age
   get { return mAge; }
   set { mAge = value; }

  private string mName;
  public string Name
   get { return mName; }
   set { mName = value; }

  public override string ToString ()
   return this.Name + " : " + this.Age.ToString();

Now we create a Persons class inheriting from generic ObservableCollection

public class Persons:ObservableCollection<Person>
  public Persons ()
   base.Add(new Person("Corrado",22));
   base.Add(new Person("Giulia",1));
   base.Add(new Person("Stefania",10));

We now use ObjectDataSource to create Persons instance and have it available as DataContext inside xaml.

<?Mapping XmlNamespace="tds" ClrNamespace="Demo" ?>
<Window x:Class="Demo.Window1"
  <ObjectDataProvider x:Key="Persons" ObjectType="{x:Type tds:Persons}"  />
  <DataTemplate DataType="{x:Type tds:Person}">
   <StackPanel Orientation="Horizontal">
    <TextBlock Foreground="Red" Text="{Binding Name}" FontSize="14" />
    <TextBlock Foreground="Blue" Text="{Binding Age}" FontSize="14"/>


   <ListBox ItemsSource="{Binding Source={StaticResource Persons}}" />    
Red lines are namespace to Xml mapping instructions (syntax is probably going to be modified on next CTP)
Magenta line is the ObjectDataProvider definition that allows us to use Person list as datasource on the listbox.
Green lines indicates that both ObjectDataProvider and DataTemplate objects are part of Windows Resources.
DataTemplate (in blue) describes how the UI should display the Person instance (otherwise the ToString() method it will be invoked) in this case I decided to use a StackPanel containing two textblocks both binding to Person properties.
Data are databound to listbox by ItemSource attribute (in orange)
Datatemplates are required because on WPF ItemsControls (like ListBox) are not limited to strings but can contain virtually everything.

Just a simpe demo, but as you know, we all need a starting point... :-)

posted @ lunedì 1 gennaio 0001 00:00 | Feedback (54)

This is my personal blog.
These postings are provided "AS IS" with no warranties, and confer no rights.