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.
Related
(I am newbie and this is probably a duplicate question.)
To see which control is clicked on form, I have a method like this;
public void IdentifyControl(object sender, System.Windows.Input.MouseEventArgs e)
{
Control ctrl = sender as Control;
if (ctrl != null)
SelectedControl = ctrl;
this.Cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle);
MessageBox.Show(""+ctrl.GetChildAtPoint(System.Windows.Forms.Cursor.Position).ToString());
}
and trying to call it from mouse click.
In my first attempt, I added this function to main forms MouseClick event but it only worked for form itself but controls in form. Then I tried to create a general click event and use by Mouse class.
The main point I stuck is that I couldn't create the suitable parameters Mouse.AddMouseUpHandler(DependencyObject element,
MouseButtonEventHandler handler)
Its probably because of I don't know event handling but maybe I am all in a wrong way.
You can create a mouse click event just double clicking on any buttons, labels, etc, and the Visual studio will create a code for you, like this:
private void YOUROBJECT_Click(object sender, EventArgs e)
{
}
Just put the method you want into this click event.
Hope this can help you!
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
I want to select all text into a textbox just when it got focus , there is a property named SelectOnEntry in wpf texbox or an equivalent?
If Not how to implment it ?
Attach handler for GotFocus event:
<TextBox GotFocus="TextBox_GotFocus"/>
and in handler:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
((TextBox)sender).SelectAll();
}
If you need to do it in pure XAML way, you can create an attached property to select all text on focus. Refer to the solution here.
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
}
How to force paint after setting Enabled = false in a C# windows usercontrol ?
Use EnabledChange event for the UserControl..
private void userControl_EnabledChanged(object sender, EventArgs e)
{
if (! Enabled)
Invalidate(); // ask the control to redraw itself
}
Note: put this code inside the userControl class, not in the form.
Good luck!
If you are using winforms:
myControl.Invalidate();
myControl.Update();
If you're talking about WPF UserControls, you can just subscribe to the IsEnabledChanged event of your control and call InvalidateVisual() in the event handler.