Jun 22, 2009

Two sessions for Code Camp this weekend

I have two sessions scheduled for the Code Camp event in San Diego this weekend

Touch 101

http://www.socalcodecamp.com/session.aspx?sid=e927884d-6bb6-4e13-9554-ee426e573f0f

XAML Panel discussion

http://www.socalcodecamp.com/session.aspx?sid=4046c382-190d-40f6-b86b-af0dd307b9ae

If your interested come and check it out. It’s free.

Jun 16, 2009

Binding to custom attached properties

Another quick post. In an effort to blog more I am trying to blog quick snippets I come across instead of full blown manifestos.

Colleagues of mine at InterKnowlogy came across an interesting binding edge case the other day and I thought it would make a decent post. In the past I have needed to bind to attached properties (although it has been rare). Typically I was doing something like this :

<DockPanel Height="100" Width="100">
<TextBlock DockPanel.Dock="Left"
Text="{Binding RelativeSource={RelativeSource Self}, Path=(DockPanel.Dock)}" />
<TextBlock DockPanel.Dock="Right"
Text="{Binding RelativeSource={RelativeSource Self}, Path=(DockPanel.Dock)}" />
<TextBlock DockPanel.Dock="Top"
Text="{Binding RelativeSource={RelativeSource Self}, Path=(DockPanel.Dock)}" />
<TextBlock DockPanel.Dock="Bottom"
Text="{Binding RelativeSource={RelativeSource Self}, Path=(DockPanel.Dock)}" />
</DockPanel>


Where I am binding to the attached property “DockPanel.Dock.” The basic trick here is to add the parentheses around the path to indicate you are binding to an attached property.



The edge case we came across this past week was binding to a custom attached property. Something like this



<TextBlock Custom:MyAttachedProperty.CustomString="Some String" 
Text="{Binding RelativeSource={RelativeSource Self}, Path=(MyAttachedProperty.CustomString)}" />


Where I am binding to “MyAttachedProperty.CustomString.” This however doesn’t work as posted. It will throw and error stating it cannot find type MyAttachedProperty. The solution may be obvious to most but we were stuck on it for a few minutes.



You have to add the namespace prefix to the property in the binding in order for the Binding markup extension to be able to resolve the custom type and find the attached property.



So the example above is corrected like this:



<TextBlock Custom:MyAttachedProperty.CustomString="Some String" 
Text="{Binding RelativeSource={RelativeSource Self}, Path=(Custom:MyAttachedProperty.CustomString)}" />


Like I said, might be obvious to most but it isn’t something I had done before so it took a few minutes to discover the correct way to do it.

Jun 9, 2009

Controlling Audio playback in XAML

Just a quick post (mainly for my own reference). I had the need to do some simple audio playback in a WPF application recently and wanted to trigger the playback declaratively.

In the  past any media playback I have done through code behind of some sort. In this scenario the audio files were going to be more dynamic and all I needed to perform was a simple play operation. I didn’t have the need to support pausing, fast forward, rewind, etc. Just play the clip.

A little poking around on the tubes didn’t turn up much useful. Hence why I am posting this entry.

I found that if you use the MediaElement control you can trigger it using a MediaTimline element in a BeginStoryboard action.

First declare your MediaElement and simply give it an x:Name (don’t specify the source, LoadedBehavior, UnloadeBehavior etc..)

<MediaElement x:Name="mediaElement" />  


Then wherever you want to trigger the animation (on a button click in my case) put the following event trigger



<Button.Triggers>
<EventTrigger RoutedEvent="ButtonBase.Click">
<BeginStoryboard>
<Storyboard>
<MediaTimeline Source="Resources\AudioFiles\easy.wma" Storyboard.TargetName="mediaElement" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>