Monday, March 03, 2008

WPF: Adventures in Formatting

Problem: I needed to have a really long string text in a bound WPF ListView column wrap instead of running on until the end of the text.

Solution: After doing quite a bit of searching around I was unable to find a good concrete example that worked in my particular case. The closest example I found looked something like this:


<ListView Height="Auto" Name="lsvErrorSummary2" ScrollViewer.CanContentScroll="False">

<ListView.View>

<GridView>

<GridViewColumn Header="Message" Width="200" DisplayMemberBinding="{Binding Path=Property}">

<GridViewColumn.CellTemplate>

<DataTemplate>

<TextBlock TextWrapping="Wrap" Text="{Binding}" />

</DataTemplate>

</GridViewColumn.CellTemplate>

</GridViewColumn>

</GridView>

</ListView.View>

</ListView>


Maddeningly enough this doesn't work as I spent at least an hour trying to find some magical combination of properties that would get it to work. The reason this doesn't work seems to be the fact that when we use the "DisplayMemberBinding" to bind the column WPF is using the binding for both the data and how to display the data, or at the very least WPF is just using the default for displaying the bound data. That being said I was able to solve it by making some changes.

The first thing I did was to remove the DataTemplate out into a Resources area (Window.Resources would work) and make sure to give it an x:Key="" attribute. After doing that I pulled out the GridViewColumn.CellTemplate tags as well. All that's left to do is to set the Text attribute of your DataTemplate's TextBlock equal to your real binding property (i.e. Text="{Binding Path=Message}" and point the CellTemplate attribute to the DataTemplate's x:Key (i.e. CellTemplate="{StaticResource CustomListViewItems}"). The end result should look similar to below:


<Window.Resources>
<DataTemplate x:Key="CustomListViewItems">
<TextBlock TextWrapping="Wrap" Text="{Binding Path=Message}" />
</DataTemplate>
</Window.Resources>

<ListView Height="Auto" Name="lsvErrorSummary" Width="511" ScrollViewer.CanContentScroll="False">
<ListView.View>
<GridView>
<GridViewColumn Header="Message" Width="200" CellTemplate="{StaticResource CustomListViewItems}">
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>

So far so good, now here's the hard part: this only works well when the column has a fixed width, but the TextWrapping setting will fail to take effect otherwise. In order to have it work with a flexible width a little bit of hacking is required. The easiest way to do this is using the ActualWidth property on the GridViewColumn you're wrapping. The following code shows doing this with two columns in the ListView:


((GridView)myListView.View).Columns[1].Width =
myListView.ActualWidth - ((GridView)myListView.View).Columns[0].ActualWidth - 5

With this code I'm using Columns[1] as the column that wraps and I am determining the width with a little padding on the end (hence the "- 5"). There may be a more effecient way of doing this, but the math is pretty straight forward and you can run this code whenever the ListView would resize in order to maintain the text wrapping.

Monday, January 14, 2008

She's Here!

Catherine Celeste Scolamiero was born at 8:11pm 1/14/2008, weighing exactly 7 lbs. and 20 in. of length. Everyone remarked at her long and beautiful dark hair. She is doing very well.

Stefanie is also doing well and currently recovering.

Chasing Chad

Chad Myers and his wife recently had a baby, and we're following them right up with one of our own. My wife is currently in labor with our baby and we're blogging to notify our friends and family with updates. The internet is a wonderful thing!

Thursday, January 10, 2008

The Joys of AppDomains

Problem: We need the ability to launch a .NET executable that has ShadowCopyFiles turned on by default in the AppDomain.CurrentDomain.

This is an issue because, from what I can tell, since .NET 1.0 there has been no way for an assembly to modify the settings in the primary AppDomain. The last time I looked at this issue was back in 2004 when Bob Aman and I did some serious work on dynamically loading/unloading AppDomains on the fly. We enabled ShadowCopyFiles on the created AppDomains so we could update the assemblies loaded on the fly as well. Delving into these depths of the CLR and Fusion turned out to be a major undertaking as we had to slog through one of the most poorly documented portions in all of .NET. In the end we were able to work it out and everything came together.

Solution: This time out, three versions of the .NET framework later, I was seriously disappointed to see how little this area has improved. Again it was back to the answer-less message boards and Google groups to try and scrounge up an answer. Fortunately instead of being several months worth of work it was only a few days before I was able to find an answer: Create a launcher executable so simple that it is extremely unlikely to ever change then have that executable create a new AppDomain with ShadowCopyFiles enabled. The launcher will then run .ExecuteAssembly() on the executable we want to have ShadowCopyFiles enabled.

Pros: The portion that we really want to be modifiable will be. Also the solution is pretty elegant as the launcher is reusable anytime we run into this particular situation.

Cons: This solution is really just a work-around (a kludge in the eyes of some) because we can't really change the settings on the default AppDomain a process gets. The launcher won't be modifiable at runtime, so it's really important that it's extremely simple and unlikely to change.

Sources: Here's a list of a few places with helpful information on this problem and related problems.

  • How to Set ShadowCopyFiles for CurrentDomain - This is a post that describes the same problem I was trying to solve. There weren't really any helpful answers posted here, however it does have some discussion of the problem.
  • Get rid of EXE/DLL locks by using ShadowCopyFiles appropriately - This post has some excellent sample code for the Launcher.exe. It's a bit hard to read at some points.
  • AppDomain Shadow Copy Explained - A great treatment of the ShadowCopyFile basics by Bart De Smet.
  • Plug-in Manager - The article on fully dynamic plug-ins by Bob Aman back in 2004. This documents several lesser known quirks of the AppDomain system and contains some great explinations for the way the whole process works.
  • AppDomain and Shadow Copy - A blog entry by Microsoft's Junfeng Zhang that captures some of the ShadowCopyFile quirks. It's important to note that the API calls he mentions at the end have all been depricated and SHOULD NOT be used.
  • AppDomain Question - An expert discussion of the particular problem faced here and suggestions of alternative ways to solve it. An excellent source for information.

Tuesday, January 08, 2008

Welcome Back

I know it's been over a year since I last posted here, but I am back, and look forward to blogging some more. It's good to be back, and I can't want to post some new content dealing with the 3.5 framework and VS2008. There will definitely be more to come soon, in the meantime check out a new blog I've added to my must read list:

Chad Myers' Blog - RSS

Tuesday, December 19, 2006

Snow and happiness

Merry Christmas everyone!

Friday, November 10, 2006

A good story

I recently had a slowness issue keeping my old project from going into full BETA. The issue would show up on Windows 2k3, however not on XP Pro. Essentially what was happening was that a certain page in the project was taking 9 - 30 seconds to load with small amounts of data (between 30 and 100 records), obviously an unacceptable amount of time. Trace.axd revealed that the page itself was loading and processing in ~.12 seconds, however it was taking the rest of the time to render. This was a big deal, and beyond the scope of my code to debug, so I decided to get a little backup from Microsoft to find out why there was such a huge discrepency between Win XP and Win 2k3, as well as what was taking so long in the pre-render through rendering phases. Well three days later we found that the issue was within one of our component vendor's controls (we use DevExpress), and specifically the licensing call. As it turns out the licensing call was being made recursively every time the page was hit. This execution "explosion" was the cause of our problems. So we contacted DevExpress, provided them with all of Microsoft's data and our own data (traces, dumps, etc.), and to our surprise we were given a bit of a run around by their support people.

At this point things look bleak (vendor giving you the run around with a critical piece of software), however all was not lost. The hero of the story on the DevExpress side would be their CTO: Julian. We contacted him with our troubles, he got to the bottom of it, we received a release fixing the licensing issue, and we were informed that in the future they would be rewriting the component in question in order to fully optimize it for speed. So in the end everyone was a winner, I have a product well on the way to production, DevExpress really came through for us (all is definitely forgiven there!), and all DevExpress users will probably be very impressed with their performance in the 6.3 release.

Tuesday, September 26, 2006

How hard can it be people?!?

Alright, you have .NET, you have ORACLE, and you have LOB fields (BLOB/CLOB, i.e. a place to store huge amounts of data for anything (BLOB) or for up to 4GB of text (CLOB) for those who don't know). How hard can it be to make a relatively coherant set of simple conversions, particularly for things over 32kb in size?!?! Obviously it isn't easy, or we wouldn't have to go through all of this mess. Needless to say I'm more than a little ticked off at both Microsoft and ORACLE on this one. It should be pretty normal to allow people to store things like files, images, and large strings on the database, and we've been doing it for years. So why does it have to be so hard to do? Why can't we just have a simple conversion from the .NET type to the ORACLE parameter? Why isn't there a simple way for us to pass BLOB parameters in .NET? It's time to get on the ball over there at ORACLE and Microsoft; give us a good solution.

Thursday, September 21, 2006

Updates, DevExpress, Licensing, etc.

First of all I want to apologize for it being so long since I've updated. Work's been crazily busy, and the last thing I've had time for is updating blog entries. I would, however, like to post on a couple of things.

First a heads up. If you are currently working with DevExpress controls there may be a problem in Windows 2003 Server with their ASPxGrid's licensing call. I've noticed this with the site I'm building and when hosted on Win2k3 it takes almost three times longer to complete rendering of the grid on the server side. This issue may affect all their controls as well. I am currently working on that with their support team and Microsoft. If this is really a problem I'll post about it again and let you know.

Also, speaking of DevExpress, I actually tried what was described in this post and I have to say, Julian B, you rock! He actually took the time to talk to me and I believe him that the performance issues I've discussed will be looked into and resolved. Again, huge kudos to Julian. DevExpress is a good company, and though I have some issues with their controls (VS2k5 documentation) I am consistantly impressed by their commitment to customers. In other words, buy their stuff.

Okay, now for some cross posting blog mentions:
-Joe Duffy: When you have one too many threads, or want to do multi-AppDomain instantiation (say it with me, Rich-Client Plugins) this is the blog for you. I look forward to more cool stuff coming from Joe.
-Junfeng Zhang: This just confuses me sometimes, but it's a good detailed blog about the .NET Framework.
-Mark Russinovich: Mark is here for when Windows just makes you say WTH?
-Chris's Corner: Want some Vista hate? Go here, and not only learn what to hate about Vista but also why, when, and how much.
-Tom Hollander: Wow, I mean just wow. The P&P folks are the best at Microsoft. They're actually making things that are causing enterprise level development to be better, easier, and cheaper. Who would have thought? Power to the people Tom, power to the people.
-Peter Bromberg: Who is this guy and why does he rock so much? I dunno, but keep up the good fight Pete. He's a great dose of humor, politics, and in your face programming. If the code doesn't work Peter will beat you and your code until it does, and you'll like it!

Alright, that's it for now.

Wednesday, July 19, 2006

100% Pure MicroLive

I know it's been awhile, and I'm sorry about that.

Lately I've been checking out the "Live Revolution" over at www.live.com to figure out what all the buzz is about and to keep tabs on where The Borg are going technology wise for the more main-stream. This, scarily enough, lead me to create a "LiveSpaces" site (currently MSN Spaces, but that will change just like messanger I'm sure).

Now the more I think about this Live stuff, especially in light of Verizon's FiOS initiative I am slowly becoming convinced of a new web revolution, one where some of our old web-dreams will come true. In particular I think that the web will become a very likely place for very rich-client style apps and maybe even a host for XAML/AJAX apps with full rich client functionality available without the pain of Flash and the postbacks of server-side code. Who knows, this may lead to even more unstructured work environment possibilities, though I get the feeling that Europe, Japan, and possibly Austrailia are already there (please comment to me and let me know if they are). Anyways, I don't know whether or not these will be realities until my generation becomes management, however the idea of "flex-work" instead of just "flex-time" seems to answer some of the slave to the job via technology issues of today. Though I think about management, and they just won't want to give up control. It's an intersting though though.

Anyways, let me know what you think.