I have the problem that I want to add an event from XAML directly to another class.
The standard class, which is used, is the MainWindow.
In my situation I want to define, which class should be used for the event.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
public class differentClass
{
public differentClass()
{
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
}
}
Maybe someone can help me, how I can use the event from the second class without any code in the MainWindow.
There is a Behavior class for this purpose. You will need to add the reference to the System.Windows.Interactivity in the project: How to add System.Windows.Interactivity to project?
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
public class CustomWindowHandlerBehavior: Behavior<Window>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Closing+= Window_Closing_Event;
}
protected override void OnDetaching()
{
AssociatedObject.Closing-= Window_Closing_Event;
base.OnDetaching();
}
private void Window_Closing_Event(object sender, System.ComponentModel.CancelEventArgs e)
{
//...
}
}
using this behavior in XAML:
<Window
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<i:Interaction.Behaviors>
<local:CustomWindowHandlerBehaviour />
</i:Interaction.Behaviors>
<Window/>
Related
I have been messing around with c# in Visual Studio (rather new to it) and have been trying to build an application using WPF and I cannot seem to figure out how in my environment to update my WPF view when a button is clicked. I have tried to cull my code down to the relevant information
I have the following scenario in my .cs file
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
using (VMS.TPS.Common.Model.API.Application app = VMS.TPS.Common.Model.API.Application.CreateApplication("null", "null"))
{
Execute(app);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
static void Execute(VMS.TPS.Common.Model.API.Application app)
{
Window window = new Window();
MainWindow mainWindow = new MainWindow(app);
mainWindow.evalButton.Click += Eval_Click //Button defined in .xaml
//Add a bunch of items
window.ShowDialog();
}
public static void Eval_Click(object sender, EventArgs e)
{
//need to add some more stuff to mainWindow and update window
}
}
My MainWindow.xaml file has class defined as .MainWindow and the MainWindow.xaml.cs file is as follows
public partial class MainWindow : UserControl
{
private VMS.TPS.Common.Model.API.Application _application;
public MainWindow(VMS.TPS.Common.Model.API.Application Application)
{
_application = Application;
InitializeComponent();
}
}
If you want your View layer to update on button press, you can re-assign the DataContext. For example:
public static void Eval_Click(object sender, EventArgs e)
{
this.DataContext = new MyDataContext();
}
But if you're following the MVVM pattern, your DataContext should inherit from the INotifyPropertyChanged interface and you can also just call a PropertyChangedEventHandler event to update specific bindings in the View layer. For example:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public void UpdateView()
{
NotifyPropertyChanged("foo");
NotifyPropertyChanged("bar");
}
...
public static void Eval_Click(object sender, EventArgs e)
{
(this.DataContext as MyDataContext).UpdateView();
}
I am not familiar with using event handlers, and I was wondering if anyone had or could direct me to some code that shows how to use an event handler that will execute code on the Close/Closed event?
I know this can be done because of this answered question:
Run code on WPF form close
But I need some direction.
Thank you =)
It's just this XAML
<Window ... Closing="Window_Closing" Closed="Window_Closed">
...
</Window>
and code for both the Closing and Closed events
private void Window_Closing(object sender, CancelEventArgs e)
{
...
}
private void Window_Closed(object sender, EventArgs e)
{
....
}
If you want to do it all from code behind put this in your windows .cs file
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Closed += new EventHandler(MainWindow_Closed);
}
void MainWindow_Closed(object sender, EventArgs e)
{
//Put your close code here
}
}
}
If you want to do part in xaml and part in code behind do this in xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Closed="MainWindow_Closed">
<Grid>
</Grid>
</Window>
and this in .cs
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void MainWindow_Closed(object sender, EventArgs e)
{
//Put your close code here
}
}
}
The above to examples you can apply to any form in a xaml app. You can have multiple forms. If you want to apply code for the entire application exit process modify your app.xaml.cs file to this
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnExit(ExitEventArgs e)
{
try
{
//Put your special code here
}
finally
{
base.OnExit(e);
}
}
}
}
You can override the OnExit function in App.Xaml.cs like this:
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
protected override void OnExit(ExitEventArgs e)
{
//do your things
base.OnExit(e);
}
}
If you are using C# on Microsoft Visual Studio, the following worked for me.
In your Window.cs file
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Name_Space
{
public partial class Window : Form
{
public Window()
{
InitializeComponent();
//...
}
private void Window_Load(object sender, EventArgs e)
{
//...
}
private void Window_Closed(object sender, EventArgs e)
{
// Your code goes here...!
}
}
}
In your Window.Designer.cs file add this line to the following method
...
private void InitializeComponent()
{
...
//
// Window
//
...
this.Closed += new System.EventHandler(this.Window_Closed); // <-- add this line
}
...
Here's a portion of the standard Label control in WinForms:
public class Label : Control
{
protected override void OnTextChanged(EventArgs e)
{
...
}
}
I'd like to override the OnTextChanged event but I'm not sure of the best way.
Should I derive a subclass from Label class and then override the function like this?
public class Class1 : Label
{
protected override void OnTextChanged(EventArgs e)
{
MessageBox.Show("S");
}
}
If so, how and where should I add this class?
If not, how can I override functions which are defined inside a control?
This is the way you can override the method for control. As you have done is absolutely right but detailed implementation is here.
This is the form part
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TestForm : Form
{
MyLabel newLable;
public TestForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newLable = new MyLabel();
newLable.Height = 30;
newLable.Width = 40;
newLable.Text = "hello";
this.Controls.Add(newLable);
}
}
}
You can use MyLabel from toolbox also.
And MyLabel class is
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MyLabel:Label
{
public MyLabel()
{
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Label Clicked");
}
}
}
I have been trying to find a good answer to this question, but can't seem to find one. I have an ASP.NET page that derives from a base page, like this:
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This works fine
}
}
And the base page:
public partial class MyBasePage: Page
{
}
protected DateTime GetLoginTime()
{
// Do stuff
return loginTime;
}
Now I have a user control on that page that needs to call my method...Like this:
public partial class TimeClock : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This does not work!
}
}
As you can see, I cannot call my base method, for obvious reasons. My question is, how can I call this method from my user control? One work around I've found is like this:
var page = Parent as MyBasePage;
page.GetLoginTime(); // This works IF I make GetLoginTime() a public method
This works, if I make my function public instead of protected. Doing this doesn't seem like a very OOP way to tackle this solution, so if someone can offer me a better solution, I'd appreciate it!
TimeClock inherits from UserControl, not from MyBasePage so why should TimeClock see the Method GetLoginTime()?
You should keep your UserControl out of your Page stuff. It should be decoupled in OOP speak. Add properties to set values and delegates to hook into events:
public partial class TimeClock : UserControl
{
public DateTime LoginTime{ get; set; }
public event UserControlActionHandler ActionEvent;
public delegate void UserControlActionHandler (object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.ActionEvent!= null)
{
this.ActionEvent(sender, e);
}
}
}
Page
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime();
TimeClock1.LoginTime = loginTime;
TimeClock1.ActionEvent += [tab][tab]...
}
}
(this.Page as BasePage).MethodName()
I'm new to GTK# (and desktop development for that matter) and I can't figure out what seems like to be a simple task. :(
I can't get a simple date picker to work. I have a main window with a single text box entry and a single button. When the button is clicked it opens a new window with the calendar widget and when the user double-clicks a date it then should return the selected date to the text box entry on the main window.
Here is my code, what am I missing?
MainWindow.cs
using System;
using Gtk;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
private DateTest1.CalendarTest datePicker;
protected void OnButton1Clicked (object sender, EventArgs e)
{
datePicker = new DateTest1.CalendarTest();
datePicker.DestroyEvent += new DestroyEventHandler(datePickerDestroyed);
datePicker.ShowAll();
}
public void datePickerDestroyed(object sender, EventArgs e)
{
entry1.Text = datePicker.DatePicked.ToString();
}
}
CalendarTest.cs
using System;
namespace DateTest1
{
public partial class CalendarTest : Gtk.Window
{
public DateTime DatePicked;
public CalendarTest () :
base(Gtk.WindowType.Toplevel)
{
this.Build ();
}
protected void OnCalendar1DaySelectedDoubleClick (object sender, EventArgs e)
{
var datePicker = (Gtk.Calendar)sender;
DatePicked = datePicker.Date;
this.Destroy();
}
}
}
You have to use the Destroyed event, not the DestroyEvent ;)
That is, use this:
datePicker.Destroyed += new EventHandler(datePickerDestroyed);
See also this question.