Command only works if you edit the code while the program is running

Asked

Viewed 15 times

0

Xaml

 <ItemsControl ItemsSource="{Binding Foldres}"
                          FontSize="14"
                          Foreground="{StaticResource SecondaryTextColorSolidColorBrush}"
                          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          ScrollViewer.VerticalScrollBarVisibility="Auto"
                          Margin="0 20 0 0"
                          Padding="0 0 5 0"
                          HorizontalAlignment="Stretch"
                          Grid.Row="1">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border>
                            <TextBlock Text="{Binding Name}"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       MaxHeight="25"
                                       FontSize="14"
                                       LineHeight="25"
                                       TextAlignment="Left"
                                       Cursor="Hand">
                                <TextBlock.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Remove"
                                                  Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page}, Mode=FindAncestor}, Path=DataContext.RemoveFolderCommand}"/>
                                    </ContextMenu>
                                </TextBlock.ContextMenu>
                                 <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDown">
                                        <!--<Behaviors:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page}}, Path=DataContext.SelectedFolderRightClickCommand}"
                                                                       PassEventArgsToCommand="True" />-->
                                        <Behaviors:CallMethodAction MethodName="SelectedFolderChange"
                                                                    TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page}}, Path=DataContext}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </TextBlock>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.Template>
                    <!-- Add Scrollview -->
                    <ControlTemplate TargetType="{x:Type ItemsControl}">
                        <Border x:Name="Border"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer Margin="0"
                                          Focusable="False"
                                          Padding="{TemplateBinding Padding}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                          VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                          CanContentScroll="{TemplateBinding ScrollViewer.CanContentScroll}">
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </ScrollViewer>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled"
                                     Value="False">
                                <Setter TargetName="Border"
                                        Property="Background"
                                        Value="Gray" />
                                <Setter TargetName="Border"
                                        Property="BorderBrush"
                                        Value="Silver" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </ItemsControl.Template>
            </ItemsControl>

Viewmodel

public class ViewModel
{
    public ICommand RemoveFolderCommand { get; set; }


    public ViewModel()
    {
        RemoveFolderCommand = new RelayCommand(AçãoASerFeita);
    }

    private void AçãoASerFeita()
    {
        ;
    }
}

Relaycommand

    public class RelayCommand : ICommand
{
    #region Private Members

    /// <summary>
    /// The action to run
    /// </summary>
    private Action mAction;

    #endregion

    #region Public Events

    /// <summary>
    /// The event thats fired when the <see cref="CanExecute(object)"/> value has changed
    /// </summary>
    public event EventHandler CanExecuteChanged = (sender, e) => { };

    #endregion

    #region Constructor

    /// <summary>
    /// Default constructor
    /// </summary>
    public RelayCommand(Action action)
    {
        mAction = action;
    }

    #endregion

    #region Command Methods

    /// <summary>
    /// A relay command can always execute
    /// </summary>
    /// <param name="parameter"></param>
    /// <returns></returns>
    public bool CanExecute(object parameter)
    {
        return true;
    }

    /// <summary>
    /// Executes the commands Action
    /// </summary>
    /// <param name="parameter"></param>
    public void Execute(object parameter)
    {
        mAction();
    }

    #endregion
}

Error message:

Erro que está acontecendo

The problem is that the command of Contextmenu is finding the ICommand DataContext.RemoveFolderCommand for 2 reasons.

1- I use the same Binding just below it : <Behaviors:CallMethodAction MethodName="SelectedFolderChange" TargetObject="{Binding RelativeSource={RelativeSource AncestorType={x:Type Page}}, Path=DataContext}" />, Out there are 4 more commits in my shampoo and they all work.

2- That command performs correctly if the following happens: If with the application running, I edit the "Hot relaod" shampoo, for example by removing the Mode=FindAncestor the command Contextmenu works normally again, so if I re-load the application it stops working again. Soon if I run the application again and again edit the command adding again the Mode=FindAncestor the command back to work, and if I give Reload again in the application it stops working until I edit again.

In short, I have one command that only works when I edit it in the shampoo while the application is running.

Does anyone know how I can fix this?

I have tried: To use the visualstudio, To use the blend, Restart the two programs, Restart the Machine, Rewrite that part of the code.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.