user1910087
user1910087

Reputation: 61

Invoke search in Avalon Editor using Button click

I am using AvalonEditor inside a window dialog(WPF). Window dialog host TextEditor control. I have Find button explicit on Window. On click of Button, Search should work inside TextEditor. Can you please suggest how to bind Button Find to invoke TextEditor Search.

Currently, I have edited the TextEditor constructor to install the SearchPanel. And when Ctrl + F is pressed inside TextEditor, default search dialog appears. I want the same thing to work on Button click, but using MVVM approach.

Please suggest.

WPF XAML code

<Window>
<StackPanel Grid.Row="0" Grid.Column="1"  Orientation="Horizontal" HorizontalAlignment="Left">
            <Button Name="FindButton" Content="Find" Margin="2" Style="{DynamicResource ButtonFooter_Style}" >
                <Button.CommandBindings>
                    <CommandBinding Command="ApplicationCommands.Find" CanExecute="CommandBinding_CanExecute">
                    </CommandBinding>
                </Button.CommandBindings>
            </Button>            
        <avalonEdit:TextEditor Grid.Row="2" Grid.Column="1" Name="textEditor"
                FontFamily="Consolas"
                FontSize="10pt"                                
                SyntaxHighlighting="XML" ShowLineNumbers="True" >
        </avalonEdit:TextEditor>
</Window>

TextEditor.cs class constructor already been edited

public TextEditor() : this(new TextArea())
    {
        Search.SearchPanel.Install(this.TextArea);
    }

to have find feature enabled on pressing Ctrl + F

Now I want Find button to invoke Search feature without pressing Ctrl + F.

-Thanks

Upvotes: 0

Views: 918

Answers (2)

Dimm
Dimm

Reputation: 11

Following the idea in https://stackoverflow.com/a/12399756/5041912 you can also add a Boolean DependencyProperty that can be set in the ViewModel and toggled by a button click

in the View Model:

private bool _openSearchPanel;
public bool OpenSearchPanel { get => _openSearchPanel; set => this.RaiseAndSetIfChanged(ref _openSearchPanel, value); }

private void OpenSearchPanelCmd()
{
    OpenSearchPanel = !OpenSearchPanel;
}

in the MvvmTextEditor:

public bool OpenSearchPanel
{
     set
     {
         this.searchPanel.Open();
         this.searchPanel.Reactivate();
     }
}

and in the View:

<local:MvvmTextEditor OpenSearchPanel="{Binding DataContext.OpenSearchPanel, ElementName=xxxx}"

Open AvalonEdit SearchPanel with a button click

Upvotes: 0

tretom
tretom

Reputation: 635

In code behind...

create a member:

private readonly SearchPanel searchPanel;

in your constructor, or in any block that suits your needs (it needs to happen only once):

searchPanel = SearchPanel.Install(textEditor);

and then in the event handling code:

        searchPanel.SearchPattern = "eg. your selected text";
        searchPanel.Open();
        searchPanel.Reactivate();;

Upvotes: 1

Related Questions