WPF Reflections

January 10, 2008  2:38 PM

ListView – finding the non selected item

Mark Shurmer Profile: MarkWPF


What I mean by the title is that sometimes when you are using a ListView, you want to find out the row that was used rather than the selected row.
The most likely scenario for this is when you have a button (or similar) as a column in the ListView. You want to be able to respond to the button click event and do something in relation to the row that it was clicked on.
Now you may (or may not) know that when you click on a button in this scenario, the row is not selected. This means that you cannot rely on using the currently selected item in the listview.
What you need to do is find the item pertaining to the row of the button that has been clicked. Actually this is quite easy because the button is part of the visual tree for the row, so you can just traverse up the visual tree to find it!

An example of how you would do this is:

private void btnAddSymbol_Click(object sender, RoutedEventArgs e)
                   DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is ListViewItem)) { dep = VisualTreeHelper.GetParent(dep); }
if (dep == null)

SymbolList item = lvSystemWatchList.ItemContainerGenerator.ItemFromContainer(dep) as SymbolList;
if(item != null)
                  // do something


December 21, 2007  2:57 PM


Mark Shurmer Profile: MarkWPF

Have you discovered InputBindings?

They allow you to specify a connection between an input device and a command. Examples of input devices are keyboard and mouse, and examples of commands being anything implemented with ICommand.

Therefore, you can add an InputBinding to take advantage of commands that you have already defined and bound in your CommandBindings section, eg:

MouseBinding MouseAction=LeftDoubleClick Command={x:Static gcmd:RoutedCommands.ViewMyPosition} />
           <>KeyBinding >Key=>“B”  Modifiers=“Control”  Command=“ApplicationCommands.Open” />

This allows you to double click the mouse and invoke the custom command ViewMyPosition, and press Control + B to invoke the built-in command Open

December 19, 2007  10:45 AM

Textbox with dynamic inlines

Mark Shurmer Profile: MarkWPF

Have you, like me recently, discovered that you need to create specialised textblocks and add them to an existing TextBlock?

One example would be adding hyperlinks into a block of text.
The TextBlock element has the Inlines property to allow you do that. You can add as many Inline derived elements as you like. So you can add things like plain text, richer text, hyperlinks etc.
You can do it in code, and using static xaml. However, what is more challenging is how to do it through data binding.
You would like to do the following:
        <Inlines Source=”{Binding Path=blah}” />

Unfortunately, you can’t, as Inlines is not a DependencyProperty to start with.

One obvious solution is to subclass the TextBlock, and add a DependencyProperty of type List<Inline>. This works a treat, and the class is :

public class RichTextBlock : System.Windows.Controls.TextBlock
public static DependencyProperty InlineProperty
     static RichTextBlock()
//OverrideMetadata call tells the system that this element wants to provide a style that is different than in base class
DefaultStyleKeyProperty.OverrideMetadata(typeof(RichTextBlock), new FrameworkPropertyMetadata(
                       InlineProperty =
DependencyProperty.Register(“RichText”, typeof(List<Inline>), typeof(RichTextBlock), 
new PropertyMetadata(null, new PropertyChangedCallback(OnInlineChanged)));
List<Inline> RichText
get { return (List<Inline>)GetValue(InlineProperty); }
set { SetValue(InlineProperty, value); }
           public static void OnInlineChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
if (e.NewValue == e.OldValue)
                     RichTextBlock r = sender as RichTextBlock;
List<Inline> i = e.NewValue as List<Inline>;                      
(r == null || i == null)
                      foreach (Inline inline in i)

I can’t help but feel that this should be an attached property somehow…..

December 13, 2007  4:46 PM

ListView views – part 1

Mark Shurmer Profile: MarkWPF

What seems to be forgotten about the ListView control in WPF, at least by some people, is that it’s more than just a replacement for the DataGridView.

The point behind it is that it separates the data from the way it is presented, so you can have the same data presented in different ways, ie details or icon or list. It seems obvious, but it has been lost in some circles.
The presentation is controlled by the View property, which then creates a ViewBase derived class to do the presenting.

The gotcha is that the ListView in WPF (versions 3.0 and 3.5) only ships with one view – the GridView.
So can you add others, of course you can.

You simply have to derive a new class from ViewBase , draw the elements in the list , plug the new class in and bingo.

Part 2 will go through how you do that!

December 12, 2007  3:14 PM

ListView – scrollbars gone?

Mark Shurmer Profile: MarkWPF

Have you ever lost the scrollbars in a ListView?

What I mean is, there lots of rows in the ListView, but it’s not displaying any scrollbar, even if you set the ScrollViewer.VerticalScrollBarVisibility property? An example of this is:

<ListView Name=”lvInstruments” ScrollViewer.VerticalScrollBarVisibility=”Auto” ScrollViewer.CanContentScroll=”True” >
    <rest of listview stuff…./><
<Canvas HorizontalAlignment=”Center” VerticalAlignment=”Center”>
<TextBlock Text=”The instrument may have been invalid or the search too ambiguous” Visibility=”Hidden” Name=”txtError” Style=”{DynamicResource TextBlock Error}” Canvas.Left=”5″ Canvas.Top=”20″ />

Here the canvas is used to locate a textblock over the listview, and though that could have been done other ways, it’s just an exampleWhat you will find with the above example is that there are no scrollbars!
To fix, just change the StackPanel to a WrapPanel, and bisto :-)

December 11, 2007  12:37 PM

WPF and Silverlight

Mark Shurmer Profile: MarkWPF

Just how do WPF and Silverlight fit together, if indeed at all?

Well, version 1.0 of Silverlight was released a little while, and to be honest left me cold. Support was lacking for LOB applications, and also developing the code behind in c# – therefore no great interest.

Version 2.0 (aka 1.1 until recently) looks a lot more interesting.
It seems that inclusion of form and layout controls is on the list of things to include, not to mention coding the code behind in c#.

The other question for me was whether we can code in xaml and place that code in a windows wpf program, in an intranet xbap and a silverlight web application without any copying and pasting or fiddly little changes, or having to nastily write xslt transforms to achieve this.

December 7, 2007  11:30 AM

Template bindings

Mark Shurmer Profile: MarkWPF

If you want to, for example, to override a control template for a control in WPF, it is quite easy and very powerful.

One great thing is that you can specify a templatebinding instead of a (slightly) convoluted syntax to specify the property you are binding to. An example:

<TextBox Text={Binding RelativeSource={RelativeSource TemplatedParent}, Path=ValueString}
This points the text property, in your template, to gets it’s value from the property called ValueString in the xaml that specifies the textbox instance, however it can be changed to:
<TextBox Text={TemplateBinding Path=ValueString}

Much simpler isn’t it.
It also has the benefit of being more efficient than the first version as well, it justs gets better doesn’t it.

However, there is a downside, as I found to my cost yesterday
It is one-way only, so if you keep wondering why changes are not being propagated back, wonder no more.
If you want two-way, then you need the first version unfortunately

December 6, 2007  3:42 PM

Current item in binding

Mark Shurmer Profile: MarkWPF

The question i was faced with was how to bind to a current item in an array. Why? I wanted to display the result of the ToString() method on the whole object – it should display something like ‘buy 100 abc.o @ 50′

I had an array (of Equities, but it could have been anything), and some xaml binding to it:

<StackPanel DataContext={x:Static EquitiesModel.Equities}>
<TextBlock Text={Binding} />
<ListBox ItemsSource={Binding}IsSynchronizedWithCurrentItem=True />

That didn’t work because the {Binding} binds to the whole array, and i just needed the current item. It turned out to be some syntax that I hadn’t seen since XPath:

<StackPanel DataContext={x:Static EquitiesModel.Equities}>
<TextBlock Text={Binding /} />
<ListBox ItemsSource={Binding}IsSynchronizedWithCurrentItem=True />

Lo and behold, it used the ToString() method, and I was happy!

December 3, 2007  11:40 AM

Attached events

Mark Shurmer Profile: MarkWPF

Firstly, what are they?

I say they are custom events that you can place into the tree in WPF to add extra functionality without subclassing controls.

The pure MSDN definition is:
“An attached event allows you to attach a handler for a particular event to some child element rather than to the parent that actually defines the event, even though neither the object potentially raising the event nor the destination handling instance define or otherwise ‘own’ that event in their namespace.”

However, you do have to read that quite a few times!
So I’ve broken it down into a few key elements:

  1. It is a normal WPF routed event
  2. They are custom events that mean you don’t have to subclass existing classes
  3. It is not a language event (with += & -= handlers in c#), you use AddHandler and RemoveHandler instead
  4. You can put them into attributes like standard system events

Hope that’s clear :-)

November 30, 2007  6:01 PM

Acropolis – where has it gone?

Mark Shurmer Profile: MarkWPF

It looks like Acropolis has been folded into the core .net framework

If you look at the blogs of the Acropolis team, that much is clear.
Whether it will feature, and when will be a totally different question however!
Let’s hope it is soon – before VS2010 out of preference.

What can we do in the meantime?

Well I can point you to the SCSF contrib project on codeplex which provides a lot of the smart client/cab functionality for wpf: http://www.codeplex.com/scsfcontrib

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: