Month: October 2015

WPF Snippet – Setting a Binding on a Converter Parameter Property

A somewhat techy post today.

I came across a problem at work recently – I was attempting to bind the result of a converter to the command parameter of a button. Note that below code is written as an example of what I was attempting, and isn’t actually live code anywhere. Hopefully, my fantastic naming convention will give that away.

Here’s what *didn’t* work:

<Button Command="{Binding AwesomeCommand}" CommandParameter="{Binding Converter={StaticResource ShinyToAwesomeConverter}, ConverterParameter={Binding ShinyItem}}"/>

This threw up an error:

a ‘Binding’ cannot be set on the ‘ConverterParameter’ property of type ‘Binding’

After some googling around, it turns out that the solution is to use a multibinding converter. My converter was updated to implement IMultiValueConverter, and became the following:

public class ShinyToAwesomeConverter : IMultiValueConverter
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
           if (!(values[0] is Shiny))
              throw new ArgumentException("Value should be Shiny.");

           return new Awesome(values[0] as Shiny);

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
           throw new NotImplementedException();

I then updated my xaml as follows:

<Button Command="{Binding AwesomeCommand}">
          <MultiBinding Converter="{StaticResource ShinyToAwesomeConverter}">
               <Binding Path="ShinyItem"/>

So there you have it. Use a multibinding converter to bind a value to a converter parameter.

Start being miserable, October is here.

Quick, stop doing anything you enjoy! It’s october. Which in the UK, means that this is the time of year that everyone has just started noticing that the days are getting shorter. The weather usually is dull and rainy, because we’re heading towards winter. Which is often the same as spring and autumn here. If you weren’t already depressed enough about that, you’re also supposed to attempt to stop smoking (because Stoptober is a thing). In addition, you’re now also supposed to stop drinking – because this years thing is also ‘Go sober for October‘.

What set this post off was receiving an email at work about Working on Waste – with the line ‘Prepare to love your leftovers because this October is Working On Waste Month.’

Don’t get me wrong – all these things are valid and good lifestyle changes. Hence why I have provided links. I just don’t get why October is the month where all this needs to happen. It’s depressing enough already, without laws coming in to make us pay 5p for a carrier bag!

In addition, this seems to be the time of year when the guilt tripping for charitable causes reaches new heights. So, not only are you depressed due to the diminishing sunlight, miserable because you’ve had to give up your enjoyable yet naughty habits, you’re now supposed to wallow in your own guilt constantly, because you’ve already given to charity as much as you can, and you’ve run out of any more to give to other worthwhile causes, or you feel you have given enough.

Was October just unlucky enough to have a name that fits in with these causes? Did this happen to October because people weren’t feeling miserable enough about the end of the summer holidays? Is it a last ditch money grab before people spend far too much on Christmas shopping?

Personally, I am almost 2 months into converting over to vaping instead of smoking. I don’t drink all that much anyway. I’m quite good with not having leftovers due to limited storage space and meal planning. I’ve made my charitable donations this year. In fact, I am behaving quite well, all things considered. I even started Christmas shopping last month to spread costs.

So, I’m going to have fun instead. #havefunbecauseoctober is my thing this month. So there.