Archive for the ‘WPF’ Category

Attaching Events to ListViewItems in WPF

01:13 PM

I was looking for a way to attach a MouseDoubleClickHandler to the implicitly generated ListViewItems in a databound control:


<listview ItemsSource="{Binding Instances}" />

For some strange reason all the mouse events seem to be directly routed instead of bubbling like other input events. Only some magic of the framework causes them to be raised for every hit control. This causes the sender to be totally worthless, since it is always the “current” object.

(more…)

A quick sketch of asynchronous View Models

01:13 PM

The How to make a loading graphic in WPF XAML? question on stackoverflow reminded me, that I wanted to write a short piece about the WPF infrastructure I’m currently writing. A first part was the Doing GUI architecture the Right Way where I describe the basic advantages of the View Model. Today I’m going to show some details of how this enables very smooth removal of background tasks from the UI Thread. Beware that this is work in progress, so edges might be rough.

The basic structure is three-fold: the Model where the actual data resides, the View which interacts with the user and the View Model as a intermediate layer. I will start out with describing the View, which is really thin. Afterwards I’ll talk a bit why the Model alone is not enough. In the last part I’ll describe my approach to filling the gap.

(more…)

WPF ResourceDictionary Syntax

11:52 AM

The current top-hit on WPF ResourceDictionary on Google has a minor error in the usage of WPF’s ResourceDictionary class. Hopefully I can save someone the ten minutes of hate I had while searching for the cause of the strange error the Studio threw at me.

Wrong:


<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="TextStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    <!-- other styles can appear in the resource dictionary too -->
    <Style TargetType="{x:Type Button}"
           x:Key="ButtonStyle">
        <Setter Property="Background"
                Value="#feca00" />
    </Style>
</Application.Resources>

Right:


<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="TextStyle.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <!-- other styles can appear in the resource dictionary too -->
        <Style TargetType="{x:Type Button}"
               x:Key="ButtonStyle">
            <Setter Property="Background"
                    Value="#feca00" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

Note the difference in where the additional <Style>s go.

Doing GUI architecture the Right Way

09:03 PM

WPF binding is great, but directly binding the View to the Model has its limitations. As soon as the Model’s and the View’s structure diverge (which happens quite fast, I was surprised myself) the direct binding approach falls apart. The first example where I hit this was a tree of items which should be displayed and selected in a (decorated) list. Data binding just doesn’t cut it. The solution lies in creating a so called ViewModel.

(more…)

How to style a WPF TabControl

01:42 PM

A very good example of how to style a WPF TabControl.

From Brandon Cannaday at Switch on the code.

I use this Style to surround each TabItem Content with a ScrollViewer:


        <Style TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel Grid.Row="0" IsItemsHost="True" />
                            <ScrollViewer Grid.Row="1"
                                VerticalScrollBarVisibility="Auto"
                                Margin="5,5,5,5"
                                Background="White">
                                <ContentPresenter ContentSource="SelectedContent" />
                            </ScrollViewer>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Reference Project: New User Interface for Simulation Tool

07:24 PM

Soil.NET detail

Soil.NET is a port of an old DOS application written in Pascal to the .NET framework. It is a one-off programming project for the civil engineering office Steinhauser, who are useing the application to support writing expertises in the area of vibration protection and seismology. While the mathematical core could be taken almost literally, the use of a modern GUI toolkit WPF enabled important improvements in user interface experience:

(more…)

WPF & Namespacemapping

06:18 PM

This happened to me when I tried to import a Namespace of an Assembly into my XAML Page:

This sucks: “clr-namespace:My.Sub; assembly=MyAssembly

This works: “clr-namespace:My.Sub;assembly=MyAssembly

See the difference? Unbelievable, but there is just a space between the ‘;’ and “assembly”. Sure, it’s not possible to use Trim() during parsing. Or there is another reason I dont know why.

 

Wow Wow Wow!!! – Wii Remote and WPF

08:16 PM

Wii Remote for a

  • Whiteboard
  • Finger Tracking
  • Head Tracking

http://johnnylee.net/projects/wii/

COOOOLLLLL!!!!

And for all WPF Friends:

http://www.cynergysystems.com/blogs/page/rickbarraza?entry=connecting_to_the_wii_control

Creating a Pushbutton from a Icon

03:15 PM

Today I posted the first article about my recent WPF experiences to CodeProject: “WPF: IconButton – Creating a little push button from a icon.”

Abstract: While there are a few demo videos how to create nice buttons with Expression Blend, I didn’t find a straight-forward description how to do a nice button from a image. This article tries to fill this gap and a bit more. The IconButtonStyle takes the contents of the button, decorates them with a little shadow and when the user presses the butten, it generates a subtle animation “pushing” the content into the screen.

Demo Application

WPF: Embedding Images in the Assembly

09:43 AM

Yesterday, I searched for a possiblity to include PNGs into my assemblies and access tem from WPF/XAML. The only stuff I found was Chad Campbell’s “Point an Image to an Embedded Resource“. Chad recommends using embedded resources and manually extracting them with Assembly.GetManifestResourceStream and PngBitmapDecoder in the code-behind. Yuck! That would mean giving every Image a Name and jump through hoops to share memory and what not.

Today I looked at it again and… (more…)