I have TextBlock I have declared it in Xaml with its Tapped event.Now i wanted to raise this event manually from code without actually Tap on it from external input.
private void TxtBlkMessages_Tapped_1(object sender, TappedRoutedEventArgs e)
{
// want to raise it manually
// some storyboards animation is present in it
}
I have defined it in xaml like this..
<TextBlock Name="TxtBlkMessages" Tapped="TxtBlkMessages_Tapped_1" />
More precisely i want to raise it from viewmodel on basis of some condition.
If you want it to raise manually then simply u can call the method
Example :
private void CallManually()
{
TxtBlkMessages_Tapped_1(null, null);
}
Just call:
TxtBlkMessages.RaiseEvent(new TappedRoutedEventArgs());
Related
My English skill is poor because I'm not a native English speaker.
My application has a MainWindowView and MainWindowBehavior also MainWindowView has control(Editor) as the following code.
<MainWindowView>
<Grid>
<TabControl>
<TabItem>
<Grid>
<Editor x:Name="editor"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
<i:Interaction.Behaviors>
<behaviors:MainWindowBehavior/>
</i:Interaction.Behaviors>
</MainWindowView>
MainWindowBehavior uses the property of Editor in the LoadedEventHandler of MainWindowView.
The following code shows the above logic.
protected override void OnDetaching()
{
this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
base.OnDetaching();
}
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.Loaded += AssociatedObject_Loaded;
}
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
this.mainWindow = sender as MainWindow;
// run time error
this.mainWindow.editor.Parser.ParsingFailed += Parser_ParsingFailed;
}
But compiler shows run time error because of the value of the Parser property of the editor is null.
I tried to initialize the parser property of the editor at the Constructer, OnApplyTemplate function, Loaded EventHandler but 3 cases all called late than Loaded EventHandler of MainWindow.
And as a result, generate run time error.
I think that the Loaded EventHandler of the editor must be called early more Loaded EventHandler of the MainWindowBehavior. But in fact, the sequence reverse.
I don't know why the sequence reverse.
How can I change the loading sequence as I thought?
Thank you for reading.
Maybe you cannot change the sequence of events, but you can sure change the way you listen to these events. I suggest you to hook-up to an event in your mainWindow that will indicate you when its editor property is set.
Your code would become:
private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
this.mainWindow = sender as MainWindow;
// Here we don't access mainWindow.editor anymore, we hook-up to an event instead
this.mainWindow.OnEditorPropertyChanged += MainWindow_EditorPropertyChanged;
}
private void MainWindow_EditorPropertyChanged(object sender){
{
var mainWindow = sender as MainWindow;
if (mainWindow.editor != null) {
mainWindow.editor.Parser.ParsingFailed += Parser_ParsingFailed;
}
}
And in your MainWindow, make sure to raise an event when its editor property is set, for example:
public delegate void OnEditorPropertyChangedEventHandler(object sender);
public event OnEditorPropertyChangedEventHandler OnEditorPropertyChanged;
// Backing field
private Editor _editor;
public Editor editor {
get => _editor;
set => {
_editor = value;
OnEditorPropertyChanged?.Invoke(this); // Raise the event
}
}
I found the cause of my problem while following your advice.
I think the OnApplyTemplate function of the Editor is called early than the Loaded event of the MainWindowView. But, in fact, the OnApplyTemplate function of the Editor is not called.
I have been apprehending wrong about my problem. I'm sorry...
Now I would make my problem right.
The Editor is Custom-Control. I missed adding code in the Generic.xaml file the following code.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WpfControls;component/Themes/Editor.xaml"/>
</ResourceDictionary.MergedDictionaries>
Now I added the above code in the Generic.xaml and the OnApplyTemplate function of Editor is called normally also it is called early than the Loaded event of the MainWindowView.
Thank you for your help and interest in my problem.
This is for an UWP app. I only found a way to check if TextBox is unfocused but it's not working. I want to check if the TextBox has a blinking cursor so you can type in it versus when the focus is lost (no blinking cursor).
if (textBoxCool.FocusState == FocusState.Unfocused)
{
// Do something
}
else
{
// Do something else
}
TextBox has LostFocus and GettingFocus event.When TextBox's cursor blinking,it will trigger GettingFocus event and when no blinking cursor,it will trigger LostFoucs event.
.xaml:
<TextBox x:Name="textBoxCool" Width="200" LostFocus="TextBox_LostFocus" GettingFocus="TextBox_GettingFocus"></TextBox>
.cs
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
//Do something
}
private void TextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
//Do something else
}
I think you should be using events for this to work properly. You don't want to use a conditional like that because then you'll still need an event to trigger it later. There's an event called "OnPointerEntered" and another "OnPointerExited" which you can use in lieu of your if-else statement.
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.control.onpointerentered
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.control.onpointerexited
Focus returns a bool value of true if focus was set to the control or already on the control whereas false if the control IS NOT FOCUSABLE. This means you can't use it inside a conditional like that unless you set the textBoxCool to un-focusable somewhere else in code.
I have a combobox and with this I have a associated event
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
}
My comobox is populated with two items a and b
I am setting combobox8.selectedItem = x where x= a or b. My event only fires if I select a from b or b from a. It does not fire if I again select a from a.
How can I do it and what's the appropriate event to deal with it ?
Moreover I am doing it all programmatically.
It makes sense that the event doesn't fire again. The selected item doesn't change. Depending on what you actually want, there are a lot of events you can utilize. You might start with Click, or DropDown, or DropDownClosed, for instance.
It won't fire because Selected Index did not change...
Take a look at msdn documentation for a list of comboBox events:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox_events.aspx
You'll find out you can use more one depending on what you want to achieve (leave,lostfocus, [...])
Because the index did not change, the event is not fired. Since you need this processing when you are refreshing the form programmatically, call the appropriate code programmatically as well:
private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
{
ProcessComboBoxInput();
}
private void RefreshFormProgrammatically()
{
// Refresh the form here...
ProcessComboBoxInput();
}
private void ProcessComboBoxInput()
{
// Process the comboBox8 here...
}
Because its selected index changed event. From a to a nothing has been changed. You can try onclick event.
If I have a button which does something and also a double-click event on a data grid which I want to do the same thing, what is the best way to ensure that only one function has to be maintained?
Apart from doing the following, is there any fancy C# way to indicate that two events are to do the same thing?
void button1_Click(...) { MyFunction(); }
void dataGrid1_DoubleClick(...) { MyFunction(); }
void MyFunction() { // do stuff }
I suppose that you are talking about a DataGridView (WinForms) so the signature of the event DoubleClick in the DataGridView and the signature of Click event on a button control is the same.
(An EventHadler). In this case you can simply set the same method using the form designer or manually bind the event
dataGridView1.DoubleClick += new EventHandler(MyFunction);
button1.Click += new EventHandler(MyFunction);
Of course the MyFunction method should match the expected signature of an EventHandler
private void MyFunction(object sender, EventArgs e)
{
// do your work
}
Reviewing my answer after a few minutes I wish to add:
If you find yourself in a situation in which you need to differentiate between the controls using the sender object (like Control c = sender as Control; if (c.Name == "someName") ) I really suggest you to return to the first idea. Call a common method but keep the EventHandler separated for each control involved.
Using VS, in the form's designer view You can set the procedure You want to call to each control's each event in the control's properties window.
image
Just to add to what Steve said, you will want to bind these events to your function manually in the Load event of your form, instead of using the events under the lightning bolt in the properties window in the designer, like so:
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += MyMethod;
dataGridView1.DoubleClick += MyMethod;
}
void MyMethod(object sender, EventArgs e)
{
//Do Stuff
}
Also, declaring a new instance of the EventHandler class has been redundant since Anonymous methods were introduced to C#, you can just point the event directly at the method as shown above.
How do I know when the image of the picturebox change?
Is there an event for an image change?
using System;
using System.Windows.Forms;
using System.Drawing;
namespace CustomPX
{
public class CustomPictureBox : PictureBox
{
public event EventHandler ImageChanged;
public Image Image
{
get
{
return base.Image;
}
set
{
base.Image = value;
if (this.ImageChanged != null)
this.ImageChanged(this, new EventArgs());
}
}
}
}
You can add this Class into ToolBox and/or from code and use ImageChanged event to catch if Image is Changed
First make sure that the images are loaded asynchronously. To do this set the PictureBox's WaitOnLoad property to false (which is the default value).
pictureBox1.WaitOnLoad = false;
Then load the image asynchronously:
pictureBox1.LoadAsync("neutrinos.gif");
Create an event handler for the PictureBox's LoadCompleted event. This event is triggered when the asynchronous image-load operation is completed, canceled, or caused an exception.
pictureBox1.LoadCompleted += PictureBox1_LoadCompleted;
private void PictureBox1_LoadCompleted(Object sender, AsyncCompletedEventArgs e)
{
//...
}
You can find more information about this event on MSDN:
http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.loadcompleted.aspx
There are load events if you use Load() or LoadAsync(), but not for the Image property. This is explicitly set by you (the developer) and is generally speaking in 100% of your control (see notation below). If you really wanted to though (there isn't really a point, though), you can derive your own UserControl from PictureBox and override the Image property, and implement your own event handler.
Notation
I suppose one event you would want an event to subscribe to is if you are using some third-party component or control that changes the image property, and you want to implement some sort of sub routine when this happens. In this event it would be a reason to need a ImageChanged event since you don't have control over when the image is set. Unfortunately there still isn't a way to circumvent this scenario.
The Paint event is working:
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
// image has changed
}