I'm currently in need to capture the Drop event of the WPF WebBrowser control but for some reason it's not firing. If I drag a .pdf file into the control it's being displayed but the Drop event isn't firing.
Small sample:
Create a new WPF project, add this inside the XAML code of the MainWindow.xaml between the Grid tags:
<WebBrowser Name="test" />
And change the MainWindow.xaml.cs so it looks like this:
public MainWindow()
{
InitializeComponent();
test.AllowDrop = true;
test.Drop += test_Drop;
}
void test_Drop(object sender, DragEventArgs e)
{
MessageBox.Show("Hi");
}
The messagebox will not be displayed when you drop a PDF file inside the WebBrowser control. What am I doing wrong?
You should try test.AllowDrop = true;. Take a look at this Tutorial
Edit:
After a few tries and a little research i found out that no Drag event will be fired at all. But maybe this question helps you here
Related
I want to create textboxes in xaml file upon a button click through the .xaml.cs file from a loop (probably). I've calculated the margins for each textbox to appear in the panel but do not know how to bind the code. Here's the image of the xaml design view and what I'm trying to achieve. The window will have one textbox for choices appear and the loop will create another and another each time the add choice button is clicked.
Can anyone please help? I'm just learning wpf. Thank you
Assuming by Bind the code you mean how to add the TextBox to the panel. To do that, you need to add the textboxes to the Children property of the panel.
If you need to do this upon initialization of your form, then just place your code after InitializeComponent(); in the form constructor.
Here's an example how to do this programatically, responding to a click event:
Xaml:
<StackPanel Name="pn_Content" Orientation="Vertical">
<Button Click="btn_Add_TextBox_Click">Add Textbox</Button>
</StackPanel>
C#:
private void btn_Add_TextBox_Click(object sender, RoutedEventArgs e) {
TextBox tb = new TextBox();
tb.Height = 23;
tb.Width = 100;
pn_Content.Children.Add(tb);
}
and that's it.
First, place all the current text boxes in a container, for example a stack panel named stackPanel.
Then add your controls to the Children of the panel:
stackPanel.Children.Add(new TextBox { Text = "TextBox" });
please refer to these questions:
WPF: How to dynamically Add Controls in dynamically created WPF Window
Adding child controls in Stackpanel WPF C#
I am trying to prevent copy/paste within a ComponentOne WPF RichTextBox. I have read that the following code should work for WPF controls:
DataObject.AddPastingHandler(EditorBox, OnCancelCommand);
DataObject.AddCopyingHandler(EditorBox, OnCancelCommand);
private void OnCancelCommand(object sender, DataObjectEventArgs e)
{
e.CancelCommand();
}
where EditorBox is a C1.WPF.RichTextBox.C1RichTextBox.
This works fine for the other WPF controls we are using, but not for the ComponentOne RichTextBox. What happens is that the copy action is not handled by the event handler, and copy works as normal. The paste action is handled, but the text is still pasted anyway, so e.CancelCommand() has no effect.
Is this a problem peculiar to the ComponentOne RichTextBox control?
You could handle the PreviewKeyDown event and check for Ctrl+p.
After datagrid gets focused it shows some buttons on the form. But what is the event that I should use to hide those buttons after datagrid loses it's focus? I should probably mention that this project is done in .Net 2.0.
You should register on dataGridView.Leave event.
gridview.LostFocus += new EventHandler(gridview_LostFocus);
public void gridview_LostFocus(object sender, EventArgs e)
{
//Do stuff when focus is lost
}
This is a bit of a noob question.
Basically, I have a grid MainGridin my WPF window
I'd simply like to be notified when the MainGridis resized ( when user maximizes/resizes the window).
This does nothing at the moment. Am I supposed to declare and then add this SizeChanged event earlier before it will work?
private void MainGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
MyTextBox.Text = "MainGrid resized";
}
Thank you
If you have your event in code-behind and this in XAML:
<Grid SizeChanged="MainGrid_SizeChanged">
it will work.
I am trying witth the following code for keyboard command binding on WPF window:
RoutedCommand cmndSettings = new RoutedCommand();
cmndSettings.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Alt));
CommandBindings.Add(new CommandBinding(cmndSettings, mnuSettings_Click));
private void mnuSettings_Click(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("key pressed");
}
This is working fine if I place this code inside a xaml Window's cs file. But, if I place it inside a user control's cs file, which dynamically loaded in a parent window, keyboard events aren't triggering there at all. What I need to do to get it working inside a user control plz? Thanks.
To work with controls under a user control, its very important that, the containers/user control is focus-able properly. otherwise, may not work.