views:

24

answers:

2

I have a button on a view that is bound via a RoutedUICommand to a command defined in the ViewModel.

The XAML code excerpt from the view:

<Button Content="Login" Command="{Binding Login}" />

In the View's CodeBehind I add the command binding from the ViewModel to the view's binding collection:

this.CommandBindings.Add( viewModel.LoginCommandBinding );

The ViewModel itself implements the command:

public class LoginViewModel:ViewModelBase
{

    public ICommand Login { get; private set; }
    public CommandBinding LoginCommandBinding { get; private set; }

    public LoginViewModel( ) {
        this.Login = 
            new RoutedUICommand( "Login", "Login", typeof( Window ) );
        this.LoginCommandBinding = 
            new CommandBinding( Login, LoginCommandHandler, CanExecuteHandler );
    }

    void LoginCommandHandler( object sender, ExecutedRoutedEventArgs e ) {
        //Put code here
    }

    void CanExecuteHandler( object sender, CanExecuteRoutedEventArgs e ) {
        return true;
    }
}

So the command was defined with the text and name both "Login". The button itself has the content "Login". Is there a way to use the command's text as the button's content?

+1  A: 

Just bind to the Name or Text property in the command, like so:

        <Button x:Name="btnName"
                Command="{Binding ThisIsMyCommand}"
                Content="{Binding ThisIsMyCommand.Name}" />

        <Button x:Name="btnText"
                Command="{Binding ThisIsMyCommand}"
                Content="{Binding ThisIsMyCommand.Text}" />
Wonko the Sane
A: 

I don't have the infrastructure to test this right now, but my souvenir of this issue is that the button should automatically use the RoutedUICommand text as its content.

Have you tried removing the Content property ?

Timores
Yes, I have heard that this should work. That is the reason I'm asking. And no, removing the Content property from the xaml code doesn't do the trick. The button stays empty.
PVitt
OK, sorry, it really seemed too easy a question...
Timores