Adding templates to base styles in WPF

October 8, 2009 by andrewcardy

This is the first snip it of information that I’ve come across which hasn’t been widely published and its very useful when delving into mutiple styles.

So I was creating a keyboard user control in WPF. Nothing special, just a lot of buttons in a stack panel, with a toggle button for caps and shift. Thats where the styling gets interesting.

So I created a base style for all the buttons on the screen (Button and ToggleButton) which looked like this:


<Style x:Key="ButtonBaseStyle" TargetType="{x:Type ButtonBase}">

            <Setter Property="Padding" Value="5" />
            <Setter Property="MinWidth" Value="35" />
            <Setter Property="MinHeight" Value="35" />
            <Setter Property="Margin" Value="2" />
            <Setter Property="Background" Value="White"/>

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ButtonBase}">
                        <Border Name="border" BorderThickness="2" BorderBrush="DarkGray" CornerRadius="3" Background="White">
                            <Grid>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

        </Style>  

From this then created two styles which inherit from the base, for the Button:


        <Style TargetType="Button" BasedOn="{StaticResource ButtonBaseStyle}"/>

Which is simple enough, and one for the the toggle button. With the toggle button though I wanted to change the border the be red when it was checked. Simple enough I though, so I wrote out this code:


        <Style TargetType="ToggleButton"  BasedOn="{StaticResource ButtonBaseStyle}">
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter Property="BorderBrush" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

But this didn’t work, no matter how much I wanted it to there was a problem… Any guesses?

It was the fact that the base style for ButtonBase had changed the whole template, so effectively it didn’t understand the BorderBush setter property.

So I had a though and I said to my self, if you can bind a property of one element to a property of another, you should be able to do this between styles, right?

So I tried binding the BorderBush in the BaseButton template to a subsequent BorderBush and I tried this in the BaseButton:


<ControlTemplate TargetType="{x:Type ButtonBase}">
                        <Border Name="border" BorderThickness="2" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" Background="White">
                            <Grid>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
                            </Grid>
                        </Border>
                    </ControlTemplate>

.. And this in the two buttons:


        <Style TargetType="Button" BasedOn="{StaticResource ButtonBaseStyle}">
            <Setter Property="BorderBrush" Value="DarkGray"/>
        </Style>

        <Style TargetType="ToggleButton" BasedOn="{StaticResource ButtonBaseStyle}">
            <Setter Property="BorderBrush" Value="DarkGray"/>
            <Style.Triggers>
                <Trigger Property="IsChecked" Value="true">
                    <Setter Property="BorderBrush" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>

..And lo and behold it worked perfectly with only the border colours being set in inheriting styles without having to repeat the BaseButton control template.

Kudos WPF! :)

Hope this makes sense, if not leave a comment and I’ll try and elaborate!

Awesome bugs!

September 30, 2009 by andrewcardy

So,

I’ve been using WPF for a little while now, and kinda enjoying it, even though the xaml markup sometimes gives me flashbacks of the horror of writing custom asp.net controls!

Until today, when I was presented with this error in the design window:


An Unhandled Exception has occurred.

Click here to reload the designer

Details:

Object reference not set to an instance of an object.

at MS.Internal.Package.VSIsolationProviderService.CreateIsolationProvider(String identity, AssemblyReferenceProvider assemblyReferences, IEnumerable`1 assemblyFolders)

etc...

It was not a nice thing to see in the morning! So I looked through the stack trace, no clues there. Checked my code (all ok), checked my references (all there). I was stumped.

Turns out its a nice bug which can be gotten around by following some simple but random steps, to ‘coax’ the IDE out of its design error! Nice, seems MS has added morning blues to its compilers now! hehe

The steps can be found here:

http://www.jay-wick.com/2009/05/visual-studio-wpf-designer-issue-and.html

Enjoy! :)

Changes! And lots of em!

August 20, 2009 by andrewcardy

Wow, a mad couple of months, taken redundancy and had three job offers!

Just had my firm offer through the post so it won’t be long till I start work in my new role! Moving to DeepVisuals in the cambridge science park to work on some intuitive image visualisation projects! Am pretty excited!

I wonder what source control they use? GIT ftw always! :D

Makes me want to start playing with my Wii wiimote again and see what I can come up with in the mean time! :D

Need to get up to date with WPF too, quite looking forward to that actually!

Hopefully I’ll have some more interesting updates soon! :)

Andy

Searching!

July 13, 2009 by andrewcardy

Haven’t been updating recently, just been looking full time for a new job! On gardening leave from my last role!

So if anyone knows of any good entry / mid-level C# or C++ jobs going, please let me know! :)

Wii stuff

May 18, 2009 by andrewcardy

Turns out I don’t have enough time for my engine :( ho hum. Not letting it drop though, as soon as my wedding anniversary has passed, we’ve been on holiday and the house is back to normal again I’ll be on it! :D

p.s. (I have a twitter feed on the right if its interesting!)

Physics engine for Wii homebrew

April 15, 2009 by andrewcardy

In short, I want one! 

In long, as of yet I haven’t found one, so I guess I’m doing some learning about collision and reaction physics for my own! :) May take a while but I only really need to implement boxes for what I need it for! hehe

Hopefully this’ll take shape soon enough!

If I have enough time inbetween working and sorting the house out etc!

Cheap way to start Wii homebrewing!

April 7, 2009 by andrewcardy

First of all, an exellent site that I reference a lot… wiibrew.org. This site has pretty much all you need to know to start home brewing! 

Anyway, if you visit it, you’ll see you need the following items for homebrew:

  • 2-8 GB SD (not SDHC) card
  • Copy of Zelda – Twilight Princess for the Wii
  • … a Wii… umm… a PC…
  • … actually thats pretty much it!

So from the list, the only thing you may not have which could cost anything substantial is Zelda – Twilight Princess. Its ment to be a good game, so if you need it for home brew it may be worth holding on to it… but if your like me and on a budget theres a legal and completely above board way to get it for free, just for the day that you need it! Whoop!

I went to  Gamestation and bought a copy pre-owned, the great thing being is on all pre-owned games they have a 7-day money back guarrentee. Seen as I only needed it for one night and dont have any intentions of playing it, I hacked my wii, loaded the homebrew channel and then took it back the next day in exactly the same condition for my refund, which they were more than happy to give.

Don’t think you can use them as rental service though, not only is this wrong, but they take your details during the refund so they’d probably notices patterns! :)

So there you go, All I had to pay for was my 2gb SD card (only about £5.99) for loading the hacked Zelda save and the homebrew channed dol on my Wii! Whoop!

So onto the homebrew…

Just getting the wiimote working with the wiiuse library that comes with dev kit pro. Tis pretty good! Just need a good long evening to get the wiispritelib working and I can start making some nice interactive games! :D

This is where you’ll get the best games from… the true homebrew scene. NOT the arsing WiiWare channel. That really annoyed me. Its just another portal for professional developers! But now I’m getting onto a whole different post!… :)

TV Loyalty Lives!

September 9, 2008 by andrewcardy

The worlds first tv loyalty system has been launched on the Nuts TV channel! Yay! Lets hope its the first of many loyalty systems across plenty of channels! As a company we’re looking to do lots more interactive applications for sky boxes, so any ideas or comments are very welcome! :D

More NFC

September 8, 2008 by andrewcardy

Seems like NFC popularity is increasing, with an interesting trial involving TfL and o2. It seems that they have been using oyster cards embedded in 6131 NFC phones, and better still its been a success! 

90% of those involved were happy with the technology and 78% would use it more often if the service became widespread. [GizModo]

See, you hear that phone manufacturers! We want NFC on phones!! (Nice looking ones too!)

[GizModo] http://uk.gizmodo.com/2008/09/02/o2_claims_mobile_phone_payment.html

Whoop!

August 14, 2008 by andrewcardy

The series 40 Nokia 6212 NFC SDK has been released! Yay!

http://www.forum.nokia.com/info/sw.nokia.com/id/5bcaee40-d2b2-4595-b5b5-4833d6a4cda1/S40_Nokia_6212_NFC_SDK.html

As I speak, its 11% downloading and increasing. I just hope its better than the 6131 SDK, which mutilates its self at sight of Vista! Once downloaded methinks I’m going to try out our NFC framework with it. I’m sure it should work fine as its been designed pretty well, maybe a few graphics re sizes but not much else!

Should be good! I’ll update after I have a play! :)

Update!

So far so good! It has so much functionality over the 6131 NFC emulator, including P2P simulation, better external reader integration (to be used as the phone NFC antenna), reading RFID cards and tags, fully emulated OS etc!

I did have a couple of troubles getting the external card reader to work though, turns out in the end it needed the Omnikey Cardman Synchronous API as well as the latest drivers, guess that’s kinda obvious now I think about it though!

Just need some decent looking phones that people would buy now! Otherwise I’m pretty scared NFC isn’t going to be adopted any time soon!

Ho hum!