Nov
17
2008
0

WPF Toolkit DataGrid, ColumnHeader Style and Blend

I’ve spent the better part of the day trying to figure out how to style the column headers of my WPF Toolkit datagrid through Microsoft Blend. Blend is a great tool and I couldn’t imagine trying to do a WPF application without it; however it is still immature and this could be why I found it so difficult to figure out. It could just be that I’m new to Blend, WPF, and XAML and still have a lot to learn.

I’ll spare you the agony I suffered this morning and jump right into the solution.

We have a pretty basic style for the datagrid at the moment. You’ll have to forgive the visual obfuscation.

Changing the the column header style looked pretty obvious in Blend. The menu walks me through the steps to create a resource for the header style.

Give the style resource a name. In this case I am going to show a pivoted set of data with a set of frozen columns on the left and columns that accept entering data on the right. I give it the name I want and choose its destination.

When the steps are complete, I have a style in the resources dictionary for my window, but something is missing.

I’m not sure if I’m missing a step, but the target type of the resource is the generic IFrameInputElement type. It doesn’t provide any properties to set in the designer.
If I look at the XAML that was created, it doesn’t give it a type.
<Style x:Key="EnterableColumnHeaderStyle"/>

The examples I’ve been seeing have a TargetType property to allow for the attached properties to work properly. I got ahead and add my TargetType property.

As you can see, I’m still learning WPF and XAML.  ReSharper steps in and offers to help out a bit. I am glad to let it. It adds the proper namespace to the resource:

It also creates the namespace directive:

I don’t really like the namespace alias, so we’ll change it with the help of ReSharper.

Blend now knows our target type and gives us all the properties we could want to style.

We can do whatever we want with the column header now.

And, just as we’d expect, we get our new style.

We can also apply a style to a particular column.

<wpfToolkit:DataGridTextColumn x:Name="FirstColumn"   Header="Something"  HeaderStyle="{StaticResource FrozenColumnHeaderStyle }" >

Written by mark in: Uncategorized, WPF | Tags: , , ,
Oct
27
2008
3

WPF and the DataGrid Shootout – Frozen Columns

My recent project has chosen WPF as its display technology. The biggest shortcoming of WPF for a traditional line of business application is its lack of a data grid control. Luckily there are several control providers out there that are already trying to meet this need; however we are attempting to determine how closely they meet our needs.

Our two most likely candidates are InfragisticsxamDataGrid and Telerik’s radDataGrid. The third contender, which unfortunately appears to be eliminated due to being open-source, is the  data grid in the WPF Toolkit. For some reason we are scared to death of open source tools. We would rather pay someone for closed source solution and be at their mercy to fix our problems than have it open and fix it ourselves. This could be an entire post on its own. Right now I’m paying the most attention to XamDataGrid and the WPF Toolkit DataGrid, maintaining hope that we’ll value something free if it provides us the value we need.

Anyway, our first out non-standard requirement is to freeze the column and row headers so we never scroll the first column or the headers to where they are no visible.

image

The column headers were simple. Both did that right out of the box. To freeze the first column with the WPF Toolkit datagrid requires setting one attribute:

<Controls:DataGrid AutoGenerateColumns="False" Name="grid" FrozenColumnCount="1">

No problem.

For the Infragistics grid there appears to be a lot more code to write. According to a post on the Infragistics forum, column freezing is not available with the Infragistics grid. The people I have seen implementing it put two grids side-by-side and only allow one to scroll, and I assume manually scroll them vertically together.

Written by mark in: Uncategorized |
Oct
24
2008
3

Replacing TFS Diff Tool

The TFS diff tool is horrible. It a pretty bare minimum difference between the two versions. The UI is basic.

image

Nice huh? This line changed, but you’re on your own to know what characters actually changed. While tooling around in the Visual Studio Options dialog I can across the settings that would let me change the diff and merge tools.Check out Tools | Options | Source Control | Visual Studio Team Foundation | Configure User Tools…

image

I needed a another diff tool, so I downloaded a trial of Beyond Compare and plugged it in there and it worked great! Except, in 15 days it wouldn’t be working so great unless I could convince someone to shell out the $X to get us past the trial. After pinging some of my buddies about what diff tools they use, I decided to try the diff tool in TortoiseSVN. I installed it and added TortoiseMerge to the dialog.

image

Ran my compare again and Bang! A new diff dialog appears that is much better than before.

image

That is still a lot of redundant clutter for me, so switching views helps a little with that.

image

We no longer get the detail about what changed on a line, but it is so much easier to manually identify when the rows overlap, I’m not missing it.

Oct
11
2008
0

WPF Day 1

Our new project at work has chosen WPF for our UI Framework. There is a nervous feeling around the office that WPF might not be the proper choice, due to the nature of WPF or the quality of the development staff and the staff to follow. After spending a few days in WPF and developing a really rough prototype, I have to admit I am pretty sure we will put both concerns to rest. WPF has so many features that make it suitable to Line of Business applications, whatever that term truly means. While I haven’t mastered WPF in the past few days, I have a fairly good understanding of those features and usage potential.

The hardest myth to overcome is that WPF is only for the shiny, glossy, media rich applications. While it is excellent at delivering those user experiences, it is filled with numerous techniques that would provide use to any desktop application (except maybe command line applications).

I’m not going to try and do a job of completely enumerating why I’m excited about using WPF in a Line of Business application, but a brief list might help to summarize them.

Layout Managers

My WinForms experience includes mostly .Net 1.1 and VB6 over the past 10 years or so, but I do have a sprinkle of experience doing UI in Java Swing. The layout managers Java had looked like they would be a huge advantage over the absolute position of .Net and VB6. WPF introduces similar functionality with content containers.

There are several devices for laying out controls on the design surface. Before, when using the .Net 1.1 devices such as Dock it was very awkward to get the layout fluid. Controls required manual resizes and would often overlay other controls unexpectedly.

With new containers like StackPanel, Grid, DockPanel, etc. and the ability to nest them as needed, complex layouts become more manageable.

Even the controls themselves have the ability to be more flexible with their layout. Making three controls proportionally resize in WinForms required handling events, doing math to determine the new sizes based on the proportions and setting the sizes of each control. With WPF, you do the following:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" />
    <TextBox Grid.Row="1" />
    <TextBox Grid.Row="2" />
</Grid>

Control Content Containers

In WPF near everything is a container. You want a checkbox inside a button? No problem.

<Button>
   <CheckBox>Cool<Checkbox/>
</Button>

Some controls, like TextBox doesn’t accept custom content. While this doesn’t appear useful at first, but the ability to override the content generated by list boxes provides am endless array of options. I imagine this will work like a repeater in ASP.Net.

Data Binding

By far the most impressive feature of WPF, at least to me, is the ability to bind, not only data elements, but also control properties and wire these bindings to other properties. There is a new rich model for binding almost anything to almost anything else. I know this seems a little vague at the moment.

The heart of the robust data binding is the dependency property. It allows for change notification as well as a few other nice features. Since the controls of WPF have their properties implemented by dependency properties, the binding is notified when they change and can bind the appropriate values.

Data binding could be used, for example to bind the font size of a grid cell to the value which it holds. We could a custom converter to convert the value of the cell to a font size or even to a color. Likewise, you could bind the text of a tooltip to an error message produced via a validator that is run when a text box value has its value changed.

Baby Steps

There are too many features to enumerate. I am about 250 pages into Programming WPF. I usually hate buying language books and prefer something with a little more longevity, but so far it has been a really fast read. I can’t say it is going to be the last WPF book anyone will every need, but coming from zero WPF knowledge, it has shed some light on the features of WPF that I see valuable. There are many more things mentioned in the book that I forgot to mention, haven read yet, poorly or entirely incorrectly phrased. :S This is by no means an expert synopsis of WPF.

While I’ve gotten my hands slightly dirty in WPF over the past week, I haven’t even begun to leverage its full power. I plan on keeping everyone abreast of my progress, short comings, troubles and successes…hopefully more of the later.

I am considering keeping a sample project running to show where I’m at in my WPF journey, but haven’t decided if it will be of value or interest to anyone. It would most likely be a simplified obfuscated version of my current project and a testbed for running spikes.

Written by mark in: Uncategorized, WPF | Tags:
Sep
08
2008
0

ReSharper Tip of the Day: Add UsingTask

While working in an MSBuild file I notice ReSharper will help you out with using new tasks. If you use one it doesn’t know, it’ll offer to add its UsingTask statement to reference it.

It’ll throw this in at the top of the document.

<UsingTask TaskName="Fooberry" AssemblyName=""/>>
Aug
19
2008
0

ReSharper Tip of the Day: Refactor This

The people at JetBrains have their ear to the ground again. After complaining about how introducing a variable via a keyboard combination wasn’t very discoverable, JetBrains came through again with pointing out Ctrl+Shift+R will point out all refactoring available in the current context.

Written by mark in: ReSharper Tip of the Day, Uncategorized | Tags:
Aug
12
2008
0

New Bookshelf Resident: The Pragmatic Programmer

I should come up with a new title for this category. Hopefully it is not a bookshelf resident, but gets read, passed around, written in, referenced, anything but sitting around gathering dust.

After a recommendation from a friend, and anticipating an upcoming vacation, I decided to use a Barnes and Nobel gift card to add The Pragmatic Programmer to my bookshelf.

It has come highly recommended and I look forward to getting into later this week. I was pretty sure David Thomas was the origin of the Angry Monkeys. In addition, he has several other books from his pragmatic bookshelf which are all highly rated.

I’ll be sure to post more as I make my way through it.

I passed on several other books. I was also considering, in several combinations:

After seeing the alternate cover for Here Comes Everybody, I remember where I first heard about it. Jeff Atwood wrote a post about him recently. Maybe that book bumps up a few notches on my reading list now.

Written by mark in: Bookshelf, Programming, Uncategorized | Tags:
Aug
10
2008
1

Hex Color Picker Plugin for Mac OS X

Today, I was amazed yet again by the flexibility of Mac OS X. I was transitioning the color scheme from my text editor to the syntax highlighter for the blog and needed to convert the RGB values to Hex. The first couple I did manually, but realized there must be a utility out there that would do that for me. I found this nice little Hex Color picker.

What really amazed me was how easily extensible the Mac system color was. I dropped the plugin file in a folder, restarted my text editor and it was there. No installer. No registry settings. Pretty amazing.

Written by mark in: Uncategorized | Tags: , ,
Aug
10
2008
1

NHibernate and Correlated Subqueries

We needed to select elements from an entity and also multiple sums of elements from its child entities. If we wanted to do it in pure SQL, we could issue the following SQL.

select company_name,
       (select sum(payment_amount)
          from payments p
         where p.company_id = c.company_id)
       (select sum(invoiced_amount)
          from invoices i
         where i.company_id = c.company_id)
from company c
group by c.company_name

However, we wanted to try and issue it using NHibernate and its criteria model. What makes this difficult is the two correlated sub queries. If we needed only one sum, a simple projection with an outer join would work. With the second sum, that gives incorrect results.

I found brief NHibernate documentation about correlated sub queries. Additional documentation stated they are allowed in the “where“, but not in the “from” and aren’t needed in the “select” because of derived properties. I’ve lost the link to the blog article that led me to that.

The mapping for the derived property would look something like this:

<property
          name    ="TotalPayments"
          insert  ="false"
          update  ="false"
          access  ="nosetter.camelcase"
          type    ="Decimal"
          formula ="(select nvl(SUM(NVL(p.PAYMENT_AMOUNT,0)),0)
                       from payments p
                      where p.COMPANY_ID = COMPANY_ID)" />

What is interesting to note here is that everything in the formula uses real SQL and database identifiers, not entity attributes and HQL.

Now that we have the mapping created, we need something for it to map to. A simple read-only property will do.

private decimal totalPayments;
public virtual decimal TotalPayments
{
     get { return totalPayments; }
}

That’s it. If we do a simple test using a projection we can see the SQL that it’s outputting and see it gives us exactly what we want.

One thing to note is the property is NOT affected by changes made to the entity. If we add payments using the domain, it will not appear in this total. This is OK since we want it mostly for reporting, and we could probably even map the property to the field and make it private or protected to avoid the confusion to the consumers of the domain.

Written by mark in: Uncategorized | Tags: ,
Aug
08
2008
4

ReSharper Wish List: Replace Literal With…

One thing I would love ReSharper to do for me is to replace a literal string with a local variable or constant and also refactor any other usings, optionally including internal usages, of the same string literal.

I would like for it to take:

public string Foo(IDictionary<string,string> d){
     if(d.ContainsKey("foo")){
         return d["foo"];
     }
     return string.Empty;
}

…and change it to:

public string Foo(IDictionary<string,string> d){
     string key = "foo";
     if(d.ContainsKey(key)){
         return d[key];
     }
     return string.Empty;
}

That sounds very doable to me, but maybe I’m not seeing the complexity. I know ReSharper has API’s for creating your own custom context actions, but I haven’t looked into that yet.

Written by mark in: Uncategorized | Tags:

Powered by WordPress | Kredit | TheBuckmaker