views:

128

answers:

1

I'll start off and say I am not using the MVVM pattern for my WPF app. Please forgive me.

Right now I have a data template with two buttons, each binds to a different command on the CLR object this data template represents. Both use the same command parameter. Here's an example of the buttons.

<Button x:Name="Button1"
        Command="{Binding Path=Command1}"
        CommandParameter="{Binding Path=Text, ElementName=TextBox1}"
/>
<Button x:Name="Button2"
        Command="{Binding Path=Command2}"
        CommandParameter="{Binding Path=Text, ElementName=TextBox1}"
/>

I would like to refactor this into a single button that can perform either command based on a user setting like a boolean in Settings.settings. I don't have access to refactoring the CLR object itself. Also this is a Data Template there isn't codebehind for me to work with. My take is that a converter would be the best bet, but I don't know how I would put that together.

The converter would need to take in the CommandParameter, as well as the DataContext so it knows which object to execute the Commands on.

Can someone lend me a hand with this? Thanks in advance.

+2  A: 

Quick and dirty solution - put both buttons on the form and bind their visibilities to the value of the bool (one negated, obviously) - that way only the valid button will be shown.

If you do want to use a converter, though, I'd be inclined to use it on the command binding, pass in the CLR object and the value of the bool and allow it to return the right command to bind to. I see no reason to pass in the command parameter since that'll be the same either way.

XAML:

<Resources>
  <controls:CommandConverter x:Key="CommandConverter"/>
</Resources>

<Button x:Name="Button" 
        CommandParameter="{Binding Path=Text, ElementName=TextBox1}">
    <Button.Command>
        <MultiBinding Converter="{StaticResource CommandConverter}">
          <MultiBinding.Bindings>
            <Binding /><-- the datacontext CLR object -->
            <Binding ... /><-- Application setting (however you intend to get that in)
          </MultiBinding.Bindings>
        </MultiBinding>
    </Button.Command>
</Button>

Code:

public class CommandConverter : IMultiValueConverter
{        
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        CommandObject clrObject= (CommandObject)values[0]; 
        bool setting = (bool)values[1];

        if (setting)
        {
            return clrObject.Command1;
        }

        return clrObject.Command2;
    }

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

Actually if the setting bool is just in the application settings you could just use it directly in the converter and only use a normal, single value converter. Not great design since it should really stand alone, but will get the job done.

Martin Harris
Of course! <Binding/> I just failed WPF 101.
Jippers