Here I will post problems I and my colleagues met and solutions we found.

Tuesday, December 18, 2007

Changing background for selected item in WPF ListBox

When I wanted to change background color for WPF ListBox the first thing I did was creating triggers in the style:

The problem is that it doesn't work. I have to change resources instead. Here is the link that describes it very well.


http://blogs.msdn.com/wpfsdk/archive/2007/08/31/specifying-the-selection-color-content-alignment-and-background-color-for-items-in-a-listbox.aspx

Friday, December 07, 2007

Master Detail Binding in WPF

I spent hours today trying to figure out how to display Master Detail relation in two lists, if I have good old DataSet with Relation between tables. All examples I could find in Google were about objects or XML structures.

What worked is this:

Let's assume we have DataSet with two tables, MasterTable and DetailTable. We also have DataRelation named Master_Detail. The binding in code looks like that:

masterList.IsSynchronizedWithCurrentItem = true;
detailList.IsSynchronizedWithCurrentItem = true;


DataSet dataSet = CreateDataSet1();
DataView dataView = new DataView(dataSet.Tables["MasterTable"]);


Binding binding = new Binding();
binding.Source = dataView;
masterList.SetBinding(ListView.ItemsSourceProperty, binding);


binding = new Binding();
binding.Source = dataView;
binding.Path = new PropertyPath("Master_Detail");
detailList.SetBinding(ListView.ItemsSourceProperty, binding);


Now, let's say we have another relation named "Detail_SmallDetail". We would add following:

smallDetailList.IsSynchronizedWithCurrentItem = true;
binding = new Binding();
binding.Source = dataView;
binding.Path = new PropertyPath("Master_Detail/Detail_SmallDetail");
smallDetailList.SetBinding(ListView.ItemsSourceProperty, binding);

I didn't figure out how to do it in XAML yet.