How do you reference a custom command in XAML? RRS feed

  • Question

  • This is a ridiculously easy thing to do in WPF, but I can't find the analog in Metro. I have several buttons on my frame window. One of the buttons is a toggle to change the view from thumbnails to details.  The commands are global to the application and live in a class called 'GlobalCommands'.  Note that the Global Commands are consumed by other classes so there's no way that creating an instance of a command in XAML is going to work.  In WPF, the button would look like this:

    <AppBarButton Command="{x:Static local:GlobalCommands.ChangeView}"
                  Label="Switch View"/>
    However, in Metro, there's no x:Static, no TypeConverters, and IValueConverters only work on bindings, so I'm stumped.  How do you place buttons in applications that have very specific (that is, not derived from the view model) commands?

    Saturday, March 28, 2015 9:39 PM

All replies

  • I am afraid you cannot bind to static properties in Windows Store Apps. It is simply not supported.

    You could create a proxy class that exposes the static command property and bind to this property of an instance of the proxy object:

    You will of course have to create an instance of the proxy object. There is no way around this.

    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.

    Sunday, March 29, 2015 1:03 PM
  • Magnus,

    That seems to have done the trick.  I wrapped my static commands in a non-static class, created an instance of the non-static Global Commands class, and referenced the commands as members of the non-static instance.  It's very ugly, but it works.

        <models:GlobalCommandWrapper x:Key="GlobalCommands"/>
    <AppBarButton Command="{Binding Path=ChangeView, Source={StaticResource GlobalCommands}}"

    Any idea why there's no x:Static XAML directive?  I'm guessing it might have something to do with the absence of Reflection, but I'm curious why such an essential language element is missing.

    • Edited by DRAirey1 Sunday, March 29, 2015 3:53 PM
    Sunday, March 29, 2015 3:52 PM