Montag, 19. Juli 2010

C# selective clone with a generic extension method

Hey guys, ever had to clone an object - but only duplicate the properties you actually needed. Or maybe you don't want to copy the id property... Something like that, well then take a look at this.

public static class ObjectExtensions
{
 public static T SelectiveClone(this Object obj, string[] properties)
  where T: new()
 {
  T temp = new T();
  PropertyInfo[] propertyInfos = typeof(T).GetProperties();
  foreach (PropertyInfo pi in propertyInfos)
  {
   if(properties.Contains(pi.Name))
    pi.SetValue(temp, pi.GetValue(obj, null), null);
  }

  return temp;
 }
}

Well you can then do something like the following.
/* 
 Product is a class with the properties
 "title", "titleAlias", "subTitle", "description", 
 "price", "stock" and "id"
 "original" is an existing object of the class Product
*/

// The following copies all properties except "id"
Product product = original.SelectiveClone(new string[] {
 "title", "titleAlias", "subTitle", "description", "price", "stock"
});

Freitag, 9. Juli 2010

Silverlight 4: Bye bye ControlParameter

If you've been using Silverlight 4 and some of its ria stuff, you may have noticed that the ControlParameter has gone missing. Actually it has gone for good - and that's a good thing.

ControlParameter was a misfit, why? Look at the following (old) code:
<riaData:ControlParameter ParameterName="title"
                             ControlName="filterTextBox"
                             PropertyName="Text" />

Now there's no databinding, and that most certainly isn't the (new) .NET way. Check out below how it should look now, with Silverlight 4. Here we doe some data filtering with DomainDataSource.FilterDescriptor.

<riaControls:DomainDataSource Name="ExampleSource" QueryName="GetExampleQuery" AutoLoad="True">
<riaControls:DomainDataSource.FilterDescriptors>
<riaControls:FilterDescriptor PropertyPath="title"
Operator="Contains" Value="{Binding Text, ElementName=filterTextBox}">
</riaControls:FilterDescriptor>
</riaControls:DomainDataSource.FilterDescriptors>
<riaControls:DomainDataSource.DomainContext>
<domain:ExampleDataContext />
</riaControls:DomainDataSource.DomainContext>
</riaControls:DomainDataSource>

See, everything is well now. :-)

Donnerstag, 8. Juli 2010

An UI Framework for (the front-end) of Websites

There are many nice User Interface Frameworks for the Web. Some of the are made for Java (and they do a pretty good job of deuglifying it - at least for the user) but most of them can be used for every architecture, because they base solely on JavaScript (and / or JavaScript Frameworks like JQuery, MooTools, etc.) and CSS. Here's a list of a few of them.


While those Libraries are all very good and contain lots of widgets and other nice things - they are also big, hard to (learn to) use and did I say big? They are huge!

If you're designing a CMS backend or an enterprise intranet solution you just have to use one of them. But if you're "just" designing a "mid-range" website and you just like to provide a little more User Experience, those framworks / libraries are just overkill.

Now, whats the first thing you need if you start to build a website (from scratch)? A nice layout. Now there are a few options... you could spend some time building a CSS based cross-browser layout, you could just slam in a table or you could use a CSS framework. There are some very interesting, grid based CSS Frameworks. My favourite is blueprint - there are others, just do a google for "css frameworks".

Now long story short - i slammed together a little, let's say, website template, or website starter kit. This template (or kit) contains my micro ui widgets for jquery and a modified blueprint css framework.

Check a demo out here.

Now keep in mind: This is a work in progress and I'm creating this template for my own use (as well as for anyone's who likes it).

Download the package here.

Now run along and create a beautiful website!

Montag, 5. Juli 2010

Yellow Fish

Oh look, a fish!

Freitag, 2. Juli 2010

Silverlight 4: Drag and drop from desktop or image

Ok, Silverlight 4 is here and everyone's very happy and excited (well.. I think it's just more fun now to use Silverlight - especially with the nice toolkit, which can be found on codeplex). Yesterday I tried one of the new features and wow, it was pretty easy to implement drag and drop for Silverlight 4 applications.

So let's say you got a Listbox, like the following one
<ListBox x:Name="myListBox" />
Now you have to add e few things:
  • Allow Drop with AllowTrop="True"
  • An eventhandler for the Drop event
  • And finally if you want eventhandlers for DragEnter and DragLeave
The whole thing should look like the following
<ListBox Name="myListBox" 
         AllowDrop="True" 
         Drop="myListBox_Drop" 
         DragEnter="myListBox_DragEnter" 
         DragLeave="myListBox_DragLeave" />
Lets talk about the "Drop" event for a moment. Drop supplies the event handler with the arguments from DragEventArgs: Take a look at it on MSDN. Here's how I handle the event
private void myListBox_Drop(object sender, DragEventArgs e)
{
 FileInfo[] droppedFiles = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];

 for (int i = 0; i < droppedFiles.Length; i++)
 {
  this.myListBox.Items.Add(droppedFiles[i].Name);
                /* you have to access the data in the listbox via droppedFiles[i].OpenRead()
                   things like File.OpenRead(droppedFiles[i].FullName) won't work because
                   of the Silverlight 4 security model */
 }

 e.Handled = true;
}
Additionally you can use the DragEnter and DragLeave event to alert the user, that dragged files can be dropped on the object - just change the background colour or something like that (a border is quite nice as well).

Donnerstag, 1. Juli 2010

MicroGallery

MicroGallery is one of the smallest (800 bytes) gallery plugins around. There are no fancy animations (as if one's watching pictures of products, one doesn't want to wait for them to fade in, slide down or whatever ...).

What MicroGallery does is:

  • Showing thumbnails
  • Letting the user click on  the thumbnails
  • Showing the clicked thumbnail in a bigger size
  • The 'target-element' in which the big pictures will be shown can be chosen, so you'll  be able to change the background-image of any element.


Screenshot

Demonstration
Check the demo out here.

HowTo
The HTML
<div id="microgallery-view"> 
</div> 
<div class="microgallery"> 
 <div class="microgallery-element"> 
  <a href="Images/mercury.jpg"><img alt="Planet" src="Images/mercury.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/venus.gif"><img alt="Planet" src="Images/venus.gif" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/earth.jpg"><img alt="Planet" src="Images/earth.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/mars.jpg"><img alt="Planet" src="Images/mars.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/jupiter.jpg"><img alt="Planet" src="Images/jupiter.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/saturn.jpg"><img alt="Planet" src="Images/saturn.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/uranus.jpg"><img alt="Planet" src="Images/uranus.jpg" /></a> 
 </div> 
 <div class="microgallery-element"> 
  <a href="Images/neptun.jpg"><img alt="Planet" src="Images/neptun.jpg" /></a> 
 </div> 
</div> 

The JavaScript
$('.microgallery-bg').microGallery({
    view: 'body'
});

$('.microgallery').microGallery({
    view: '#microgallery-view',
    defaultSelected: 2
});

Look at the demo for css samples :-).


Download
Source code
Packed for deployment

Socialize!