When writing Silverlight applications there might one question come up from time to time: Is it possible to "feed" the application with initial parameters and if yes, how can this be done?
To do this, there are so-called InitParams. This paramaters stores the given data in key-value pairs. They can be declared this way:
website=http://blog.norberteder.com,category=wpf
This means:
- Each key-value pair uses a comma as delimiter
- A value can be assigned to the key using =
The declaration of the initial parameters can be done within the parameter tags of the Silverlight object definition:
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/DemoApp.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="2.0.31005.0" />
<param name="autoUpgrade" value="true" />
<param name="initparams"
value="website=http://blog.norberteder.com,category=wpf" />
<a href="http://go.microsoft.com/fwlink/?LinkID=124807"
style="text-decoration: none;">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181"
alt="Get Microsoft Silverlight" style="border-style: none"/>
</a>
</object>
In the App’s code behind file two events are registered. One of them is the application’s Startup-event. Within the eventhandler you have access to the StartupEventArgs which provides access to the InitParams.
private void Application_Startup(object sender,
StartupEventArgs e)
{
NeededParameters parameters = new NeededParameters();
if (e.InitParams != null && e.InitParams.Count > 0)
{
if (e.InitParams.ContainsKey("website"))
parameters.Website = e.InitParams["website"];
if (e.InitParams.ContainsKey("category"))
parameters.Category = e.InitParams["category"];
}
this.RootVisual = new Page(parameters);
}
The code above shows how you can fill up a custom object (NeededParameters) using the data given bei the Init Params. This object is used as argument for the constructor of the Page to be displayed.
That’s everything you have to do.
November 7th, 2008
in
WPF |
1 Comment »
I wrote some postings about data binding in the past (see here, here and here). It is a pretty cool feature to show data from objects without having a heavy coupling. But there was a "restriction". To format the values you have to create your own converter implementation. This takes some time.
With .NET Framework 3.5 and Service Pack 1 there is a simplier way to do this: StringFormat.
<TextBlock>First Name</TextBlock>
<TextBox Text="{Binding FirstName}"/>
<TextBlock>Last Name</TextBlock>
<TextBox Text="{Binding LastName}"/>
<TextBlock>Birth Day - Long Format</TextBlock>
<TextBox Text="{Binding Path=BirthDay, StringFormat=D}"/>
<TextBlock>Birth Day - Short Format</TextBlock>
<TextBox Text="{Binding Path=BirthDay, StringFormat=d}"/>
A possible result can be as follows:

There are much more possibilities: have a look at Lester’s blog.
September 26th, 2008
in
WPF |
No Comments »
In some cases there are problems with the clarity of small fonts. To avoid these problems you should know about how WPF renders text.
Have a look at the article Text Clarity in WPF to get an overview how WPF works in that case and how you can avoid the problems mentioned above.
August 21st, 2008
in
WPF |
No Comments »
Hibernate is one of the mosed used O/R mappers in the Java world. For .NET NHibernate is available which can be downloaded under the given link. Hibernate is some years old and works pretty fine.
I’ve used this O/R mapper already for different projects. Therefore I am receiving questions concerning NHibernate very often. As this is not my main point of interet I haven’t wrote an article about NHibernate. As I got the question "How to start with NHibernate" again for several times within the last weeks I tried to find a very good resource for beginners. And here it is:
Your first NHibernate based application
This article should give you a very good entry point if you want to learn about NHibernate. Try the sample project and you should be able to use NHibernate within your solutions.
August 13th, 2008
in
3rd Party Tools, General |
1 Comment »
To develop data based applications brings up the need to use O/R mappers for translating your relational data to objects. Regarding to WPF there might come up the problem that NHibernate does not support ObservableCollection by default.
The ObservableCollection is necessary to notify changes within a collection to the data binding system of WPF.
Accidentally I found an article that comes up with a solution:
Bridge the Gap Between Your NHibernate Collections and WPF UI
This article includes a sample project as well.
August 6th, 2008
in
WPF |
1 Comment »
I was asked, how a generic list based on a dynamic type can be loaded. Here is some code that comes up with a solution:
Type t = Type.GetType("System.String");
IList tempList = (IList)Activator.CreateInstance(
(typeof(List<>).MakeGenericType(t))
);
Console.WriteLine(tempList.GetType().FullName);
August 5th, 2008
in
.NET General |
No Comments »
I got the question how to display items within a ListView and their children in another ListView as a child item. This can be done very easily.
To create an example, we need our class hierarchy. For this we create a class called Parent, one called Child and the needed collections.
Parent
public class Parent
{
private ChildCollection _children = new ChildCollection();
public string FirstName { get; set; }
public string LastName { get; set; }
public ChildCollection Children
{
get { return _children; }
}
}
public class ParentCollection : ObservableCollection<Parent>
{
}
Child
public class Child
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class ChildCollection : ObservableCollection<Child>
{
}
As we want to work with some test data, we’ll create a data mock that will create the necessary data for us:
public static class DataMock
{
private static ParentCollection _parents;
public static ParentCollection GetParents()
{
if (_parents == null)
{
_parents = new ParentCollection();
Parent p1 = new Parent() { FirstName = "Norbert", LastName = "Eder" };
Parent p2 = new Parent() { FirstName = "Hugo", LastName = "Test" };
Child c1 = new Child() { FirstName = "Child", LastName = "Uno" };
Child c2 = new Child() { FirstName = "Child", LastName = "Due" };
p1.Children.Add(c1);
p2.Children.Add(c2);
_parents.Add(p1);
_parents.Add(p2);
}
return _parents;
}
}
Within our XAML file (of a window for example) we create a ListView and set the ItemSource property to the result of our DataMock. This is done via code behind but can also be done via XAML.
Furthermore we need a template for displaying the parent items within the List View. This template defines two TextBlocks binded to the given data source. Then there is another ListView which exists for showing the children of the parent item. For this another template is needed which defines how the children are displayed.
Here’s the code:
<Window x:Class="DotNetGui.ListViewInListViewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DotNetGui.ListViewInListViewDemo"
Title="Nested ListView Demo" Height="Auto" Width="Auto">
<Window.Resources>
<DataTemplate x:Key="ChildrenTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding LastName}" Grid.Column="0"/>
<TextBlock Text="{Binding FirstName}" Grid.Column="1"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="ParentTemplate">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding LastName}" Grid.Column="0" Grid.Row="0"/>
<TextBlock Text="{Binding FirstName}" Grid.Column="1" Grid.Row="0"/>
<ListView ItemsSource="{Binding Children}" ItemTemplate="{StaticResource ChildrenTemplate}" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" BorderThickness="0"/>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView x:Name="DataListView" ItemTemplate="{StaticResource ParentTemplate}"/>
</Grid>
</Window>
And here is the result (it’s not the niciest, but this shouldn’t be the problem):

July 16th, 2008
in
WPF |
1 Comment »
I got this question very often via email. So I’ve decided to give an answer using my blog.
DataTemplate
A DataTemplate defines the visual tree of an data item represented within a ListBox, ListView or something similiar. This can either be for an object that inherits for example a ListBoxItem or is a simple CLR object.
ControlTemplate
A ControlTemplate describes the visual tree of an control. That means how a control is represented.
Conclusio
That means: Use the DataTemplate for items which are represented within an control which has a collection of items. Otherwise use the ControlTemplate.
And yes, this is a very simple anwer to the questions I got, but it should provide the important information. If not, leave a comment.
July 14th, 2008
in
WPF |
No Comments »
Yezzzzzzz
Two days ago I was awarded the Microsoft MVP for 2008. Category: Development Platforms - Client Application Development.
I got the MVP for my efforts in the German speaking communities, my weblog and several projects I started.
I am really happy about that! Thank you Microsoft and all others who supported me.
July 3rd, 2008
in
General |
1 Comment »
Yesterday I was asked if it’s possible to implement a simple hover effekt for the WPF ListBox control. What does this mean? An item of a list box is selected when clicking it. But in this case the item should be selected on a mouse move.
To show how this can be done here is a complete example.
First of all, we need a simple data class which can be bound to our list box control:
namespace DotNetGui.ListBoxHooverSelector
{
public class Person
{
private static int _counter = 0;
public String FirstName { get; set; }
public String LastName { get; set; }
public static Person Build()
{
return new Person() { FirstName = "Test" + (_counter++).ToString(), LastName = "Tester" + (_counter++).ToString() };
}
}
}
After that we define the content of our window. This includes the list box as well as some data bound fields. Furthermore it includes a data template for the items of the list box:
<Window x:Class="DotNetGui.ListBoxHooverSelector.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ListBox Hoover Selector" Height="300" Width="400">
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="LightGray"/>
<Setter Property="Margin" Value="0 2 0 2"/>
</Style>
<DataTemplate x:Key="PersonTemplate">
<StackPanel Orientation="Horizontal" MouseMove="StackPanel_MouseMove">
<TextBlock Text="{Binding LastName}"/>
<TextBlock Text=", "/>
<TextBlock Text="{Binding FirstName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border BorderBrush="LightGray" BorderThickness="1" CornerRadius="5" Padding="2" Margin="2" Grid.Column="0">
<ListBox x:Name="PersonListBox" BorderThickness="0" ItemTemplate="{StaticResource PersonTemplate}"/>
</Border>
<StackPanel Orientation="Vertical" Grid.Column="1">
<TextBlock Text="Firstname of selected person"/>
<TextBox Text="{Binding SelectedItem.FirstName, ElementName=PersonListBox}"/>
<TextBlock Text="Lastname of selected person"/>
<TextBox Text="{Binding SelectedItem.LastName, ElementName=PersonListBox}"/>
</StackPanel>
</Grid>
</Window>
As shown in the XAML markup, we’ve created an event handler for the StackPanel which is defined for each item. This is needed to catch dthe MouseMove event.
Last but not least we have to fill up the list box with items and to handle the MouseMove event:
namespace DotNetGui.ListBoxHooverSelector
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
PersonListBox.Items.Add(new Person() { LastName = "Eder", FirstName = "Norbert" });
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
PersonListBox.Items.Add(Person.Build());
}
private void StackPanel_MouseMove(object sender, MouseEventArgs e)
{
PersonListBox.SelectedItem = (sender as StackPanel).DataContext;
}
}
}
That’s all.
Download: ListBox Hover Selection Demo
June 27th, 2008
in
WPF |
2 Comments »