Blog Home  Home Feed your aggregator (RSS 2.0)  
Eric Malamisura's Blog - Friday, July 06, 2007
Geek Ramblings
 
 Friday, July 06, 2007

So far my experience with WPF has been well met, it is an extremely flexible system with a nearly limitless possibilities with customizing controls.  However, with flexibility often comes complexity.  So I have been messing with the ListView control and was trying to figure out how to hide the column headers, is it possible?  It didn't seem like the control even supported it...But then I remembered reading about Styles and how you can set nearly any property with a style, on any control whether its nested deep in the controls hierarchy or not.

So that's exactly what I did, and you can see my XAML for that below:

   1:  <GridView>
   2:    <GridView.ColumnHeaderContainerStyle>
   3:     <Style TargetType="GridViewColumnHeader">
   4:       <Setter Property="Visibility" Value="Hidden" />
   5:       <Setter Property="Height" Value="0" />
   6:     </Style>
   7:  </GridView.ColumnHeaderContainerStyle>


If there is an easier way to do that then I am all ears, please share the knowledge!

The next step in my customizing quest was I wanted two columns, the first one to show an icon, the second one to show text.  However, the ListView control was inside of a grid column with a grid splitter so this column could get bigger and the control along with it.  Which was desired, if you make the control bigger I wanted the text that was Ellipsed due to not enough space to show. 

This presented a problem, first how do you get the second column to take up the remaining room of the control?  Secondly, how do you get the text to ellipse when the control is cutting the text off?

The first one was not so easy to solve, and I actually never came up with a solid solution, more of a hack really and it only works since in my instance I don't need to have horizontal scrolling ability inside my ListView control. So the solution was to hide the horizontal scrollbar and set the column width to a very high number, which works but it's really a dirty solution.  Until someone can offer me a better solution this is the only one I could come up with.

Note: I solved this problem without the hack in a later post, you can view it here.

Ellipse Text XAML:

   1:  <GridViewColumn.CellTemplate>
   2:   <DataTemplate>
   3:    <Label HorizontalAlignment="Stretch">
   4:    <TextBlock Text="{Binding Path=Name}" Width="Auto" HorizontalAlignment="Stretch" TextTrimming="CharacterEllipsis" />
   5:     <Label.ToolTip>
   6:      <ToolTip>
   7:       <TextBlock Text="{Binding Path=Name}" />
   8:      </ToolTip>
   9:     </Label.ToolTip>
  10:    </Label>
  11:   </DataTemplate>
  12:  </GridViewColumn.CellTemplate>


Hide Horizontal Scrollbar XAML:

<ListView ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">


Last but least, normally each ListViewItem is only large enough to fit the contents.  In order to avoid this we set the ListView.ItemContainerStyle as seen below:

   1:  <ListView.ItemContainerStyle>
   2:   <Style TargetType="{x:Type ListViewItem}"><Setter Property="HorizontalContentAlignment" Value="Stretch" />
   3:   </Style>
   4:  </ListView.ItemContainerStyle>


So that's basically it for now, if you have any cool stuff you have find be sure to send them my way.  I am gathering that WPF is going to be a huge learning curve for everyone, going to take quit a while to get elite at this stuff.  Designers will be able to have a field day, but more often than not us poor developers are left doing both design and development.

Friday, July 06, 2007 9:34:09 PM (GMT Standard Time, UTC+00:00)  #    Comments [2]    | 
 Sunday, June 24, 2007
Been messing with WPF, usually when a new language comes out to get familiar with it I write a simple test applicaiton.  Normally this is a replica of Notepad, I usually write an exact replica of Notepad with every single feature duplicated.  This tends to get me quit familiar with the new technology, from there I can move on to more interesting things.  Well with WPF this has taken me far longer than any language I have done this in thusfar, it has so many unique things in it, that are completely foreign to me and most developers.

So one thing I noticed that was quit unusual was how the RichTextBox worked, it is much much more powerful to a point where it's nearly unbelievable to the point it can be extended, I expect to see alot of new programs with a very unique text support interface coming out with this thing.  But more to the point, loading a text file into a RichTextBox in WPF is less than straight forward.

Below is how you do it:

using (Stream stream = fileDialog.OpenFile())
{
    TextRange tr = new TextRange(rtMain.Document.ContentStart, rtMain.Document.ContentEnd);
    tr.Load(stream, DataFormats.Text);
}

Now my first impressions were what the hell?  How does it work?  So on further investigation it's quit unique, the ContentStart, and ContendEnd properties are actually something called TextPointer and are indeed references and not integer values as one would expect from previous versions.  While this is not apparent it is much more elegant and gives you much more flexability at adding text in any spot you wish without changing the caret position, etc.

So basically TextRange holds these two pointers, and when you load the file from a stream it takes the contents of the stream and dumps that in between those two pointers.  So there you go, now it makes sense!

Sunday, June 24, 2007 2:44:10 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
 Monday, May 21, 2007
A typical problem that is encountered using multiple threads and events is that when you fire your event, the subscriber to that event will be running from the thread that initiated the event.  The typical way of getting around this is to check InvokeRequired in the windows form and then use Invoke of that form to Invoke the event.  Wouldn't it be nice if you could marshall that event to the proper thread before the UI gets the event?  I mean you wouldn't have to deal with it, it would just work.

So you may wonder why didn't the .NET team just build this into WinForms?  Well I wondered that to, but I watched a Channel 9 video (can't remember which one) where basically they said they didn't do this because it would put a huge performance burden if every single event had to be thread safe.  So in order to achieve optimal performance it was up the developer to only make a form/component/etc thread safe only when it needed to be thread safe.  This way performance is optimal.

With that said you can use the below code snippets that a co-worker of mine pulled from the Microsoft CAB Framework, you can use it to marshal an event to the control where the synchronization context is located.  All WinForms have a SynchronizationContext, which is used by the FireMarshalledEvent method to marshal the event to the forms thread context.

Below is a very simplified example of how this would work, I would suggest you modify it to your needs before using it:

void FireMarshalledEvent(SynchronizationContext syncContext, Delegate eventDelegate, EventArgs eventArgs)
{
    if (eventDelegate != null)
    {
        if (syncContext != null)
        {
            syncContext.Send(
                delegate(object data)
                {
                    try
                    {
                        ((Delegate)data).DynamicInvoke(this, eventArgs);
                    }
                    catch (TargetInvocationException exp)
                    {
                        //Log Error
                    }
                    }, eventDelegate);
                }
        else
        {
            eventDelegate.DynamicInvoke(this, eventArgs);
        }
    }
}

public event EventHandler<EventArgs> MyEvent;

FireMarshalledEvent(SynchronizationContext.Current, MyEvent, new EventArgs());


Monday, May 21, 2007 2:20:17 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
 Tuesday, May 08, 2007
So a couple of weeks ago I ran into a situation on a project I was working on where all projects in the solution were signed, we received a third party assembly from a vendor that was not.  We asked them to sign it but it was going to take nearly 2 weeks before we received that delivery.  In order for us to move forward I needed to have this assembly signed, but how do you sign an assembly without access to the source code?

Well a coworker of mine came up with a rather ingenious solution, and I am posting here so the knowledge can be shared.  I googled for a solution and found none so hopefully this will help people with this same situation.

So here are the commands:

ildasm /tokens /out=unsignedAssembly.il unsignedAssembly.dll
ilasm /dll /key=key.snk unsignedAssembly.il /out=signedAssembly.dll

As you can see whats happening here is you use the dissassembler to generate the MSIL code into the unsignedAssembly.il file, at which point you essentially have the code, or a form of it.  Next you use the assembler to reassemble the msil code into the binary assembly but this time you sign it using your key!

Its brilliant, it works great and I did not notice any drawbacks to doing it this way.

Tuesday, May 08, 2007 1:56:39 PM (GMT Standard Time, UTC+00:00)  #    Comments [2]    | 
 Wednesday, May 02, 2007
Been a while since I have posted, so as you can see I don't think I have a journalists defining attributes.  But in any case to the point of this post!

I was writing a test application and I needed an easy way to take any object and print all of its properties, so I wrote a tiny little function that does just that, it will take any object and build a string that can then be used however you like.  I thought I would share it since it can be helpful, the debug viewer in Visual Studio lets you do this already but if you want to take that and print it out to a log file or a text window than you would normally have to write out all of the properties by hand. In addition by doing it that way you are bound to that specific type and it just starts being a huge amount of code for something really simple.

/// <summary>
/// Gets the object view.
/// </summary>
/// <param name="objInstance">The obj instance.</param>
/// <returns></returns>
private string GetObjectView(object objInstance)
{
    StringBuilder builder = new StringBuilder();
    foreach (MemberInfo info in objInstance.GetType().GetMembers())
    {
        if (info.MemberType == MemberTypes.Field)
        {
            builder.Append(info.DeclaringType.ToString() + "." + info.Name);
            FieldInfo fieldInfo = (FieldInfo)info;
            builder.Append(": " + fieldInfo.GetValue(objInstance));
            builder.AppendLine();
        }
    }
    return builder.ToString();
}


Wednesday, May 02, 2007 2:47:06 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
 Sunday, March 11, 2007
Found something interesting on the Microsoft Research page about an automatic test generation tool that is being developed called "Pex."  Which is quit interesting, they have a screencast available for your viewing pleasure if you would like to watch it.  I think it holds a great deal of promise, when I write Unit tests that are very repetitive and it gets quit tiresome after a while of basically just copying and pasting.  Change a couple things, and then do it all over again.  This would solve that problem, but something I find kind of interesting is that it will offer a fix for certain issues. 

Here is what the Microsoft page says about it: "Pex (Program EXploration) is an intelligent assistant to the programmer. By automatically generating unit tests, it allows to find bugs early. In addition, it suggests to the programmer how to fix the bugs."

You can visit the Pex page here: http://research.microsoft.com/pex/

Sunday, March 11, 2007 1:24:17 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
 Wednesday, March 07, 2007
I had one hell of a time finding this page, all of the help pages, KB pages, etc. pointed to an outdated link on Microsoft's page.  But if you are having the same problem here is a direct link to the download.  This fixes all known issues with Visual Studio 2005 and Windows Vista, also this is not the Beta service pack that was released prior.  This is the full blown released Service Pack 1 Update for Windows Vista.

http://www.microsoft.com/downloads/details.aspx?FamilyID=90e2942d-3ad1-4873-a2ee-4acc0aace5b6&DisplayLang=en


Have Fun!!

Wednesday, March 07, 2007 3:22:48 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
Well since I have started getting involved with writing gadgets I have been looking for a nice free javascript ide to use, and I finally found one.  It's called Aptana and while it has it's issues with Vista it is a great IDE, in fact I can't believe its free its so great.  I would highly recommend checking it out, however if you plan to use it with Vista there are a couple of things worth mentioning.

You can download it here: http://www.aptana.com

Installing

  1. Right click the installer executable "Aptana_IDE_Setup.exe" and go to the compatibility tab.
  2. Check "Run this program in compatibility mode for: Windows XP (Service Pack 2)
  3. Check "Disable desktop composition"
  4. Check "Run this program as an administrator"
-OR-
  1. Double click the installer but cancel the install.
  2. Vista 'should' prompt you and ask if the program installed correctly, choose no and Vista will do the above for you but it doesn't always ask so this is kinda flaky.
Running

  1. Right click the "Aptana" shortcut and select properties and go to the compatibility tab.
  2. Check "Run this program in compatibility mode for: Windows XP (Service Pack 2)
  3. Check "Disable desktop composition"
  4. Check "Run this program as an administrator"
Everything should work perfectly fine after you perform those actions and I have not noticed anything that has not worked properly.  It does use Java which it will install for you so just beware that it will be a tad bit slower than a native application, but its not very noticeable.

Wednesday, March 07, 2007 1:59:09 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]    |   | 
 Tuesday, March 06, 2007
Well if you use Vista and you did not notice the horrible state of video drivers than you are either in denial or you don't use Vista enough.  So the long anticipated release from NVidia is finally here, this release fixes the majority of the major bug complains so far with the flickering issue when UAC pops up or CTRL-ALT-DEL is used in particular.  You can view more info about the driver on their site below.

http://www.nvidia.com/object/vista_driver_news_030207.html

I use the 64 bit version of Vista and was getting blue screens while watching movies, so far this has not reoccured with these drivers and hopefully it wont.  I still think they have a ways to go I mean XP's drivers have been tuned over a course of several years and Vista has only been tuned for a a couple of months now, so in due time I expect Vista to outperform XP.  I especially expect DirectX 10 to be a very jaw dropping experience, from the previews I have seen DirectX 10 is leaps and bounds above anything OpenGL could muster up so I cant wait till the latest releases of games hit the shelves using this amazing technology.

I bought the GeForce 8800 GTX about 1 month ago and I forgot to take picturs of it to post up here on my blog in my haste to install it and test it out.  But everyone knows what it looks like, I will take a couple shots with it in my computer and post them up here later for the curious.  I plan to rebuild my machine with some new components soon anyways, its not old its only 1 year old but I like bleeding edge and ever since purchasing my own home its harder to keep that edge with the pocket a little less empty.

Anyways enjoy the drivers!!


Tuesday, March 06, 2007 4:11:40 AM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
Copyright © 2008 Eric Malamisura. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.