Invoke search in Avalon Editor using Button click - c#

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

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();;

Related

UWP Popup class bug

I have created a simple notepad. I am unable to assign a User Control to the Child property of Popup. Every time I do it throws an exception of type System.ArgumentException. When I instantiate the User Control in code it works fine.
User control is called WrapOptionsDialog.
<Page.Resources>
<local:WrapOptionsDialog x:Key="wrapOptionsDialog" />
</Page.Resources>
<Grid>
<TextBox Name="txtbox"
IsEnabled="False"
FontSize="24"
AcceptsReturn="True"
TextWrapping="{Binding Source={StaticResource wrapOptionsDialog}, Path=TextWrapping, Mode=TwoWay}"/>
</Grid>
WrapOptionsDialog wrapOptionsDialog = this.Resources["wrapOptionsDialog"] as WrapOptionsDialog;
// Create popup
Popup popup = new Popup
{
Child = wrapOptionsDialog,
IsLightDismissEnabled = true,
};
Below in comment I'm going to link the actual project. Exception occurs after clicking on Wrap Option button. Please help.

WPF Copied Button Click not firing

I'm quite new to c# wpf and have a problem.
I have used the answer from this post to duplicate a Grid control. The grid control contains a button. It looks like it is being duplicated correctly.
When the original control's button is pressed, the click event is handled which calls a method in the window's code.
When the copy of the control's button is pressed, the click event is not fired and the method is not called. This is confusing me as I want it to call that same method.
Maybe the event handling data is not being copied properly? Is there a way around this?
Both the origional grid and copied grid (containing the buttons) are children of another grid.
Edit:
This is the xaml for the origional grid which contains a button:
<Grid Name="TempTab" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="5,5,5,0">
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke ="White" Margin="0,0,-2,0">
</Rectangle>
<Grid>
<DockPanel LastChildFill="False">
<TextBlock Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,0,3,3">Some Text</TextBlock>
<Button Width="50" BorderBrush="{x:Null}" Foreground="{x:Null}" BorderThickness="0" Margin="3,0,0,0" Click="tabdowntest">
<Button.Background>
<ImageBrush ImageSource="TopMenuBar_Close.png" Stretch="Uniform"/>
</Button.Background>
</Button>
</DockPanel>
</Grid>
</Grid>
This grid is a child of a DockPanel with name 'TabsDock'.
It is being copied with the following code:
string gridXaml = XamlWriter.Save(TempTab);
StringReader stringReader = new StringReader(gridXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid newTab = (Grid)XamlReader.Load(xmlReader);
TabsDock.Children.Add(newTab);
This is the code for the 'Click' event handler which should be called when the either the origional or the copied button's are pressed. But it is only called for the origional:
private void tabdowntest(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Button Pressed");
}
The bindigs are not set, you need to set them (comment in the orig post):
To be clear, this is only half the solution (as it stood back in 08). This will cause bindings to be evaluated and the results be serialized. If you wish to preserve bindings (as the question asked) you must either add a ExpressionConverter to the Binding type at runtime (see the second part of my question for the relevant link) or see my own answer below for how to do it in 4.0.

Element is already the child of another element when doesnt exist?

I'm trying to programatically create a button flyout, within my XAML I have:
<Page.Resources>
<Button x:Key="LaunchFlyout" Content="LAUNCH">
<Button.Flyout>
<Flyout Placement="Top">
<Grid Width="200" Height="200">
<StackPanel>
<Rectangle Fill="Red" Width="100" Height="100" />
<Rectangle Fill="Green" Width="100" Height="100" />
</StackPanel>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
</Page.Resources>
Nested within grids I have:
<Grid x:Name="launchBtn_grid" Grid.Column="1">
</Grid>
And then in my code within the Page_Loaded method I have:
bool hasContainer = localSettings.Containers.ContainsKey("appStatus");
if (!hasContainer) {
Button button = (Button)this.Resources["LaunchFlyout"];
launchBtn_grid.Children.Add(button);
}
else {
Button button = new Button();
button.Content = "LAUNCH";
button.Click += launch_btn_Click;
launchBtn_grid.Children.Add(button);
}
When I debug this, it reaches the IF statement and reaches this line launchBtn_grid.Children.Add(button); and then I get this error Element is already the child of another element.
Does anyone understand why? I have already looked and they dont already exist so I don't understand why it is giving me this error. Does anyone know what I am doing wrong?
I'm not sure in what context/use case your are doing that, but it feels weird to me to have an actual control as a Resource (not a DataTemplate, Style, etc).
If you only want to have 1 button of the 2 different template, why not switch Visibility on the 2 instead of loading controls from your code behind ?
Going forward with the idea, just add both buttons in the Grid within your XAML and switch their Visibility according to the setting you read.
There is a BooleanToVisibilityConverter within the framework to help you with this.

How do I use a textblock for navigation?

I am new to windows dev't and C#, how do make those texts i posted in the code navigate to another xaml page?
Thanks
<StackPanel>
<TextBlock Text="contact us"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
<TextBlock Text="help"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"/>
</StackPanel>
There are several ways to do this. Based on what you've described, one way would be to link an event to a Window.Show() method, to show the window. See the links I've provided here for some examples and thoughts, but basically you could insert this into a TextBlock to cause a method to happen when you do a mouse-click on it:
<TextBlock Text="contact us"
TextWrapping="Wrap"
Style="{StaticResource PhoneTextExtraLargeStyle}"
FontSize="{StaticResource PhoneFontSizeExtraLarge}"
MouseUp="MethodToCallNameHere"/>
When you write this in XAML, highlight "MethodToCallNameHere" and press F12, and it will automatically insert a method for you in that Window's code-behind. You can then put in your new window's class there with .Show().
For example, if you wanted to show a new window that is an AboutWindow class (i.e., when you created another XAML window, you named it "AboutWindow"), you could highlight "MethodToCallNameHere" and press F12.
Then, in the created method in the code-behind, you could make it look something like this, to open "AboutWindow" window when the TextBlock is clicked:
private void MethodToCallNameHere(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
// instantiate the new Window's class
AboutWindow aboutWindow = new AboutWindow();
// call the .Show() method to show the new window
aboutWindow.Show();
}
I'd recommend looking through MS's online (and free) documentation on WPF applications here. It's been very helpful to me as I've been learning.

Why is my Button event is not occuring?

I am having an issue with my button event not occuring
Basically I have cart items that are listed in the listbox. When the delete button is clicked then the item is deleted from the list box.
I tried debugging, but it seems to not even call the method for when the button is clicked.
In my ticketscreen.xaml file I specify my button in the template:
<DataTemplate x:Key="TicketTemplate">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="50">
...
<Button Name="Remove" Width="35" Height="35"
FontFamily="Resources/#charlemagnestd-regular.otf" FontSize="24"
Click="removeCartItem" Grid.Column="5"
MouseMove="Remove_MouseMove">X</Button>
...
</StackPanel>
</DataTemplate>
My List box is the following:
<ListBox Name="TicketItems" ItemsSource="{Binding}"
ItemTemplate="{StaticResource TicketTemplate}"
Grid.Row="3" Grid.ColumnSpan="6" Background="Transparent"
BorderBrush="Transparent" IsHitTestVisible="False">
</ListBox>
My method removeCartItem is in the ticketscreen.xaml.cs:
private void removeCartItem(object sender, RoutedEventArgs e)
{
Console.WriteLine("TestingCartRemove");
}
Am I missing something obvious?
Thx in adv! :)
Edit:
There seems to be something infront of it... maybe the listbox? How do I make it so that I am not clicking the ListBox, but I can click on things within the Stackpanel, which are contents of the list box.
IsHitTestVisible="False" for the ListBox is disabling the click event for the Button. It makes all content within the ListBox invisible to hit-test as well.
Are you sure is not firing? Maybe you haven't seen the output in Visual Studio Output Window. Try to call a MessageBox.Show("Test"); instead.
You have a listbox control, which leads me to believe that this is not a console application. Therefore, Console.WriteLine() will not show you anything. Try MessageBox.Show() instead.

Categories

Resources