WPF Reflections


February 18, 2008  1:20 PM

WPF incompatible with Linq for Sql



Posted by: MarkWPF
WPF

Is WPF incompatible with Linq for Sql?

Why do I suggest that?
Well Linq for Sql has a method called GetNewBindingList() which copies an EntitySet<cls> (where cls is your entity type) to a BindingList<cls>.
Unfortunately, the BindingList class is a Windows Forms binding class, not a WPF one.

So, did they forget to do the equivalent for WPF, or is there a better way buried deep in the documentation somewhere??

I’ll give you a clue, I haven’t found one
:-( 

February 17, 2008  10:54 AM

Combobox – reference not being set



Posted by: MarkWPF
C, Databinding, WPF

I’ve come across this strange little quoible.
It’s possibly just just the way I’ve been trying to use it, but it still seems odd to me

If you have a ComboBox which you are databinding to using an object, like:

<ComboBox SelectedItem=”{Binding Path=Market}” />
Where Market is a reference to another object

If you fail to initialise the object , Market in this case, the combobox won’t do it!

Obviously, the circumstances in a LOB application are more complex than the simple example above, but it does seem bizarre behaviour.


February 8, 2008  4:24 PM

WPF and WCF



Posted by: MarkWPF
WPF

I have been trying to get a three tier LOB system working over the past few weeks.

As part of that , I did discover one cool thing about WCF with relation to WPF.
That is, it’s dead easy to specify which collection class you wish to use when the WCF classes are generated on the client.

For example, for WPF you may want ObservableCollection in your business classes instead of an array or List<>
Unfortunately you can’t do it via the GUI for adding service references, but you can do it on the command line by specifying the /ct parameter when running SvcUtil, e.g.:

svcutil /edb /o:DataService /r:”c:\somewhere\mydll.dll” /ct:Utils.ObservableList`1

Here I use my own version of ObservableCollection called ObservableList in a dll called mydll.dll. If you just want to use standard ObservableCollection, then you won’t need the /r parameter


February 8, 2008  3:42 PM

ListView – why does ItemTemplate not do anything



Posted by: MarkWPF
WPF

Why does ItemTemplate not do anything?

This is a question that I have asked myself this week, in between banging my head against the wall.

What I wanted to do was apply a template to a row (in a GridView), in addition to the cell templates specified in the Columns attribute in the GridView.
Unfortunately, nothing happens :-(

If you want to do this, you have to use a style instead.
A very good example of this can be found on Bea Costa’s blog


February 5, 2008  3:38 PM

ListView – rows with combo boxes



Posted by: MarkWPF
WPF

Are you having a problem with a ListView with ComboBoxes?

Do you have the scenario where you have a ListView with a GridView, and you have an editing template for one (or more) columns?
Say your object looks like:

public class Order
{

public string Id
{
get ;
set ;
}

public CounterParty
{
get;
set;
}

}

where counterparty is a reference to a list of counterparties.
you might have a celltemplate with a combobox with it’s ItemsSource set to the counterparties list.

Are you scratching your head because every time you add a new Order (in this case), but every time you change a combo box it changes every row to be the same item in the combobox?

Try saying IsSynchronizedWithCurrentItem=”False” rather than leaving it out altogether.


January 31, 2008  1:03 PM

Combobox – using simple properties



Posted by: MarkWPF
WPF

To follow on from my previous post about Comboboxes (see binding combo boxes to objects), how do you bind to a simple int representation of a foreign key?

Lets say you have a Person object, with a member called Employee which is an int and refers to the Id on an Employee object.

When you want to bind to an Person object with an int you set:

  • SelectedValue – this points to the EmployeeId member (typically set to {Binding Path=EmployeeId})
  • SelectedValuePath – point this to the member of the Employee object that will match up to your EmployeeId member, typically this will be set to {Binding path=Id}
  • DisplayMemberPath – point this to the member of the Employee that displays something meaningful, like Name. This will then get displayed in the drop down.
  • ItemsSource – point this to the ObservableCollection that holds the list of Employees

This is all you need to do -)


January 29, 2008  4:17 PM

Combobox – using objects



Posted by: MarkWPF
WPF

I have seen a number of people getting confused about how to set up the various combobox properties for data binding.

Lets say you have a Person object, with a member called Employee which is a reference to an Employee object
When you want to bind to an Person object with an object reference you set:

  • SelectedItem – this points to the Employee member (typically set to {Binding Path=Employee})
  • DisplayMemberPath – point this to the member of the Employee that displays something meaningful, like Name. This will then get displayed in the drop down.
  • ItemsSource – point this to the ObservableCollection that holds the list of Employees

This is all you need to do :-)


January 24, 2008  10:01 AM

Hierarchial data template



Posted by: MarkWPF
WPF

Although you can do recursion quite simply with DataTemplates (see recursion example), you may want to use a Hierarchical Data Template instead. You may want to do this if you are for example connecting the Data Template up to a TreeView.

The template will look like this for a tree of files and directories:

<HierarchicalDataTemplate DataType=”{x:Type si:DirectoryInfo} ItemsSource=”{Binding Path=., Converter={StaticResource fileConv}}”>
<TextBlock Text=”{Binding Path=Name}” />
</HierarchicalDataTemplate>

Where fileConv is a static resource to a converter class that converts a FileInfo into a string, and si refers to System.IO.

The magic happens in the ItemSource property, which when it encounters a DirectoryInfo will create another template, and so on.


January 24, 2008  9:43 AM

Recursive data template



Posted by: MarkWPF
WPF

Yes, believe it or not, it is possible to have a recursive data template.

I was recently writing a ListBox that showed a directory, but I wanted the ListBox to show nested ListBoxes for subdirectories.
It turned out to be very easy with WPF:

<DataTemplate DataType=”{x:Type si:DirectoryInfo}”>
<ListBox ItemsSource=”{Binding Path=., Converter={StaticResource fileConv}}” />
</DataTemplate>

Where fileConv is a static resource to a converter class that converts a FileInfo into a string, and si refers to System.IO.

You may be thinking where is the recursion? The magic is in the fact that when the DataTemplate encounters a DirectoryInfo it will invoke the DataTemplate for that DirectoryInfo, and suddenly it just all works.


January 16, 2008  11:59 PM

Popup box



Posted by: MarkWPF
C, Development, Windows Computing, WPF

One thing that I have noticed, out there in the real world, is that people new to WPF haven’t noticed the (slightly) unsung hero Popup.
I have actually seen people spending man weeks recreating a floating window as per the popup control in WPF.

So what is it? Well it is the ancestor class for menus, tooltips and the combobox – and provides the necessary floating behaviour.

An example of using it is:

<StackPanel>
<ListBox Name=”xxx” />
<Popup PopupAnimation=”Fade” Placement=”Mouse” x:Name=”pop”>
<TextBlock Text=”ggggg” />
</Popup>
</StackPanel>

This example will show a floating window at the mouse cursor, when the mouse is in the stackpanel. Easy as pie isn’t it?


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: