1

Hi am using button click event in xaml file :

   <Button Focusable="False" Click="btnAddhighlight"
                        Grid.Column="0" Width="37" Height="37" VerticalAlignment="Center" HorizontalAlignment="Center">
            <Image Stretch="None">
                <Image.Style>
                    <Style TargetType="{x:Type Image}">
                        <Setter Property="Source" Value="/Resources/Highlighter.png"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsPressed, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" Value="True">
                                <Setter Property="Source" Value="/Resources/Highlighter_pressed.png"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </Button>

I am using button click event in xaml.cs file :

   private void Button_Click_1(object sender, RoutedEventArgs e)
    {
//I want to call relay command from this code

    }

This is my relay command :

        AddHighlight = new RelayCommand(() =>
        {
            int page = _pageNumber;
            SelectionInfo selection = _selection;

            Rect? selectedVilualInfo = null;
            if (_selection != null)
                selectedVilualInfo = ConvertPixelsToScreen(_selection.SelectedRect, CurrentZoom, _visualObject);

            ParentVM.HighlightVM.SelectedVilualInfo = selectedVilualInfo;
            ParentVM.HighlightVM.IsVisible = Visibility.Visible;
            var vm = (ParentVM.CurrentReadingVm as ReadingBookSingleVM);
            if (vm != null)
            {
                ParentVM.HighlightVM.ResetCurrentColor();
                vm.SaveHighlight(page, HighlightVM.SerializeColorToString(ParentVM.HighlightVM.CurrentColor.Color));

                vm.CancelSelection(page);
                Selection = null;
            }
        });
2
  • 1
    Wrap all this Command Inside the Method and call the function from your Button click event Commented Jun 8, 2015 at 9:35
  • Get the current context of your UserControl or a window: var vm = (<your VM type>)this.DataContext; and then call that relayCommand vm.AddHighlight.Execute(object param); Commented Jun 8, 2015 at 9:51

1 Answer 1

2

try this

 private void Button_Click_1(object sender, RoutedEventArgs e)
 {
    var vm = (yourViewModel)DataContext;
    vm.AddHighlight.Execute(null);

 }

or use eventtocommand of mvvmlight toolkit,

but with controls which have command property button and checkbox for example you have just to bind your RelayCommand to the command property

<Button Focusable="False" Command="{Binding YourCommand}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.