Ryan Mrachek
Ryan Mrachek

Reputation: 771

WPF RelyCommand binds but doesn't execute?

I have a command that binds to the view model, is enabled, but doesn't always execute. How can I go about debugging this? I used WPF Inspector and it re confirms that the binding is correct.

More Detail: I have a tab control that only executes the command for tabs to close when the tab is not selected. When the tab is selected the command will not hit.

The code is fairly standard and I can't seem to see or debug the probems.

The templated close button on TabItem

                <Style x:Key="ClosableStyle" TargetType="telerik:RadTabItem">
                <Setter Property="HeaderTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>
                                <ContentControl Grid.Column="0" Content="{Binding DisplayName}"/>
                                <telerik:RadButton Grid.Column="1" Margin="3 1 -4 0" Width="16" Height="16" Opacity="0.7" Command="{Binding Path=CloseCommand}">
                                    <TextBlock Text="x" FontFamily="Arial Rounded MT" FontSize="12" Margin="0,-3,0,0" />
                                </telerik:RadButton>
                            </Grid>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

The command:

RelayCommand _closePanelCommand;
/// <summary>
/// Returns the command that, when invoked, attempts
/// to remove this workspace from the user interface.
/// </summary>
public virtual ICommand CloseCommand
{
    get
    {
        if (_closePanelCommand == null)
        {
            _closePanelCommand = new RelayCommand(
                () =>
                {
                    this.OnRequestClose();
                }
            );
        }

        return _closePanelCommand;
    }
}

Upvotes: 0

Views: 1355

Answers (3)

Jose
Jose

Reputation: 11091

I also recommend using Snoop which is a UI debugging utility that is essential for this kind of debugging. Otherwise you're flying blind.

The thing with a Button is that if the Command binding fails(which happens silently) the Button stays Enabled so you don't know if the button is Enabled because the ICommand says so or because the binding failed.

You can also look at your output window which should tell you if the binding failed, but snoop is quite a bit easier than reading through a bunch of text in your output window :)

Upvotes: 1

blindmeis
blindmeis

Reputation: 22445

you should check the actual datacontext of your button (take Snoop). i think that the datacontext is not right in case your command get not fired.

Upvotes: 0

Rohit Vats
Rohit Vats

Reputation: 81253

I guess what your problem is you are trying to use ICommand as an INotifiableProperty which won't work like this. You need to provide the handler for your command in your ViewModel's constructor something like this -

this.CloseCommand= new RelayCommand(param => this.OnRequestClose(param));

And just write your property like this -

public ICommand CloseCommand { get; set; }

Upvotes: 0

Related Questions