How to add a Canvas event from another class? WPF C# - c#

I have a MainWindow class, and that is the main window of the app. I've created another class just below the MainWindow class, and I need to add an event to a Canvas that was created in MainWindow. The method I want to add as event is also in MainWindow.
public partial class MainWindow : Window
{
public void Moving(object sender, MouseEventArgs e)
{
bla bla;
}
public Canvas Getcanvas
{
get
{
return canvas;
}
}
}
public class Ka
{
public Ka()
{
MainWindow.Getcanvas.MouseMove += new MouseEventHandler(//HERE!!! I DONT KNOW WHAT GOES HERE, here should probably be MainWindow.Moving but I get error "An object reference.."
}
}
Please help me !

var mainWindowInstant = (MainWindow)App.Current.MainWindow;
mainWindowInstant.Getcanvas.MouseMove += new MouseEventHandler(...);

Related

Communicate between pages in wpf

I have two Pages and one MainWindow.. I load the Pages in two Frames.. Now I want to execute methods from each other.. How can I do this?
This is Page1.cs:
public partial class Page1 : Page
{
public Method1()
{
doSomething;
}
}
This is Page2.cs:
public partial class Page2 : Page
{
public Method2()
{
doSomethingElse;
}
}
In my MainWindow the following happens:
Frame1.Source = new Uri("/Source/Pages/Page1.xaml", UriKind.RelativeOrAbsolute);
Frame2.Source = new Uri("/Source/Pages/Page2.xaml", UriKind.RelativeOrAbsolute);
Is there any way, to execute Method2 from Page1.cs, and Method1 from Page2.cs?
Regards
One way to do this is through their common parent, the window.
Looking at this (modified accordingly)
public partial class MainWindow : Window
{
public Page1 Page1Ref = null;
public Page1 Page2Ref = null;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Frame1.Source = new Uri("/Source/Pages/Page1.xaml", UriKind.Relative);
Frame1.ContentRendered += Frame1_ContentRendered;
// do the same for the Frame2
}
private void Frame1_ContentRendered(object sender, EventArgs e)
{
var b = Frame1.Content as Page1; // Is now Home.xaml
Page1Ref = b;
if(Page2Ref != null) // because you don't know which of the pages gets rendered first
{
Page2Ref.Page1Ref = Page1Ref; // add the Page1Ref prop to your Page2 class
Page1Ref.Page2Ref = Page2Ref; // here the same
}
}
// do the same for the other page
}
from this question
you should be able to set a reference once a page is loaded to the other page .
Better yet, you might want to let the Pages know of their window parent and access the other page through it. Either way, is bad design, I'm telling you.
Is not a solution to be proud of, you might better look into MVVM, and go with it.
Let me know if it worked for you.

Button located under tabitem (within tabcontrol) but the handler needs to be in the main window

I have a main windows, which contains some controls including a tabcontrol. The tab control itself has multiple tabitems. Each tab item has a unique design. When the user clicks a button within one of the tab items, I want the event handler to be within the main window's .cs file. I cant figure out a way to do that, can any one enlighten me?
My syntax may not be perfect, but what about this:
public interface ITabControlWithNotifications
{
void OnButtonClicked(...);
}
public sealed class TabControlWithNotifications : TabControl, ITabControlWithNotifications
{
public void OnButtonClicked(...)
{
...
}
}
public sealed class TabPageWithNotifications : TabPage
{
private readonly ITabControlWithNotifications parentTabControl;
public TabPageWithNotifications(ITabControlWithNotifications tabControlWithNotifications)
{
this.parentTabControl = tabControlWithNotifications;
this.InitialzeComponents();
}
... ClickEventHandler(...)
{
this.parentTabControl.OnButtonClicked(...);
}
}
public sealed class TabControlFactory
{
public void Build()
{
var parentTabControl = new TabControlWithNotifications();
var tabPage1 = new TabPageWithNotifications(parentTabControl);
var tabPage2 = new TabPageWithNotifications(parentTabControl);
var tabPage3 = new TabPageWithNotifications(parentTabControl);
parentTabControl.Controls.Add(tabPage1);
parentTabControl.Controls.Add(tabPage2);
parentTabControl.Controls.Add(tabPage3);
}
}

C# How do i change a label text from another form

So i have 2 forms.
Form 1 is my main form, and form 2 is where I enter text in a textbox to be displayed on a label on form 1. Also the button to 'confirm' and actually change the entered text of my label is on form 2 which needs to stay that way.
for some reason this does not work.
Form 2 has a text-box and a button, when I press the button, it changes the string value of the designated string.
This string is linked to a label on form 1. the string is being changed so that is not the problem I confirmed this by a adding a button which pops up a message box showing the new string value.
While searching for an answer I found that is must be some sort of refreshing problem, I tried a lot of methods with no success. Only methods that did work where those who would put my button onto form 1 instead of 2.
I've been googling for 3 hours straight on how to fix this problem but either the methods don't work or they change my button from form 2 to my main form (form 1).
Please don't call me lazy I really can't find a method that works!
EDIT:
Code
GameScreen.cs
namespace _2pGame
{
public partial class GameScreen : Form
{
public GameScreen()
{
InitializeComponent();
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
playerinfoload.Show();
}
}
}
PlayerInfo.cs
namespace _2pGame
{
public partial class PlayerInfo : Form
{
public PlayerInfo()
{
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
}
}
}
Refs.cs
namespace _2pGame
{
public partial class gm
{
public static string
P1Class,
P2Class,
P1Name,
P2Name;
}
}
An approach to this very well know situation is through delegates....
In your PlayerInfo form declare
public partial class PlayerInfo : Form
{
// define the delegate type (a parameterless method that returns nothing)
public delegate void OnConfirmPlayer();
// declare a public variable of that delegate type
public OnConfirmPlayer PlayerConfirmed;
.....
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
// Check is someone is interested to be informed of this change
// If someone assign a value to the public delegate variable then
// you have to call that method to let the subscriber know
if (PlayerConfirmed != null)
PlayerConfirmed();
}
}
Then in your GameScreen form, just before showing the PlayerInfo form, set the public PlayerInfo.PlayerConfirmed to a method into the GameScreen form class
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo();
// Subscribe to the notification from PlayerInfo instance
playerinfoload.PlayerConfirmed += PlayerHasBeenConfirmed;
playerinfoload.Show();
}
// Method that receives the notification from PlayerInfo
private void PlayerHasBeenConfirmed()
{
P1NameLabel.Text = gm.P1Name;
P1ClassLabel.Text = gm.P1Class;
P2NameLabel.Text = gm.P2Name;
P2ClassLabel.Text = gm.P2Class;
}
This approach has the advantage to avoid a coupling between the GameScreen and the PlayerInfo. No need to know inside the PlayerInfo the existance of a GameScreen form and the name of its properties. You just publish a delegate that a subscriber could register to be informed of the changes and let the subscriber acts on its own code.
You need a reference to your main form and assign the textbox values each time they need to be updated.
public partial class PlayerInfo : Form
{
private readonly GameScreen _main;
public PlayerInfo(GameScreen main)
{
_main = main;
InitializeComponent();
}
public void ConfirmPlayerInfo_Click(object sender, EventArgs e)
{
gm.P1Class = P1ClassChoice.Text;
gm.P1Name = P1TextBox.Text;
gm.P2Class = P2ClassChoice.Text;
gm.P2Name = P2TextBox.Text;
main.P1NameLabel.Text = gm.P1Name;
main.P1ClassLabel.Text = gm.P1Class;
main.P2NameLabel.Text = gm.P2Name;
main.P2ClassLabel.Text = gm.P2Class;
}
}
You also need to pass the reference when the PlayerInfo form is created
private void PlayerInfoButton_Click(object sender, EventArgs e)
{
PlayerInfo playerinfoload = new PlayerInfo(this); //pass ref to self
playerinfoload.Show();
}
Note that there are other better ways to do this, but this is the easiest that I can think of.
You can probably look at events or Mediator pattern if you want something better.

How to bring window to front with wpf and using mvvm

I have a window that essentially runs a timer. When the timer hits 0 I want to bring the window to the front so that it is visible and not hidden behind some other application.
From what I can gather I would simply call window.activate() to accomplish this but with mvvm my view model doesn't have a reference to window.
A "purist" MVVM solution is to use a behavior. Below is a behavior for a Window with an Activated property. Setting the property to true will activate the window (and restore it if it is minimized):
public class ActivateBehavior : Behavior<Window> {
Boolean isActivated;
public static readonly DependencyProperty ActivatedProperty =
DependencyProperty.Register(
"Activated",
typeof(Boolean),
typeof(ActivateBehavior),
new PropertyMetadata(OnActivatedChanged)
);
public Boolean Activated {
get { return (Boolean) GetValue(ActivatedProperty); }
set { SetValue(ActivatedProperty, value); }
}
static void OnActivatedChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
var behavior = (ActivateBehavior) dependencyObject;
if (!behavior.Activated || behavior.isActivated)
return;
// The Activated property is set to true but the Activated event (tracked by the
// isActivated field) hasn't been fired. Go ahead and activate the window.
if (behavior.AssociatedObject.WindowState == WindowState.Minimized)
behavior.AssociatedObject.WindowState = WindowState.Normal;
behavior.AssociatedObject.Activate();
}
protected override void OnAttached() {
AssociatedObject.Activated += OnActivated;
AssociatedObject.Deactivated += OnDeactivated;
}
protected override void OnDetaching() {
AssociatedObject.Activated -= OnActivated;
AssociatedObject.Deactivated -= OnDeactivated;
}
void OnActivated(Object sender, EventArgs eventArgs) {
this.isActivated = true;
Activated = true;
}
void OnDeactivated(Object sender, EventArgs eventArgs) {
this.isActivated = false;
Activated = false;
}
}
The behavior requires a reference to System.Windows.Interactivity.dll. Fortunately, this is now available on NuGet in the Blend.Interactivity.Wpf package.
The behavior is attached to a Window in XAML like this:
<Window ...>
<i:Interaction.Behaviors>
<Behaviors:ActivateBehavior Activated="{Binding Activated, Mode=TwoWay}"/>
</i:Interaction.Behaviors>
The view-model should expose a boolean Activated property. Setting this property to true will activate the window (unless it is already activated). As an added bonus it will also restore a minimized window.
You could go about it in a couple of ways - adding a reference to the window could work since the viewmodel is not coupled with the view but related to it, but I don't really like that approach since it pretty much does couple your view to your viewmodel - which is not really the point of MVVM
A better approach may be to have your viewmodel raise an event or a command which the view can handle. This way the view gets to decide what UI action is associated with the command/event
e.g. simply
class SomeView
{
void HandleSomeCommandOrEvent()
{
this.Activate();
}
}
Of course how you wire this up is up to you but I'd probably try and get routed commands happening
Edit: You can't really 'bind' a simple event, since it's invoked from the viewmodel.
A simple event based example is just to add the event to the viewmodel and handle it directly ... e.g. imagine the following MainWindow with a ViewModel property
public partial class MainWindow : Window
{
MainWindowViewModel ViewModel { get; set; }
public MainWindow()
{
InitializeComponent();
ViewModel = new MainWindowViewModel();
ViewModel.ShowMessage += ViewModel_ShowMessage;
this.DataContext = ViewModel;
}
void ViewModel_ShowMessage(object sender, ShowMessageEventArgs e)
{
MessageBox.Show(e.Message, "Some caption", MessageBoxButton.OK);
}
}
Then the ViewModel can just fire the event:
// The view model
public class MainWindowViewModel
{
// The button click command
public RelayCommand ButtonClickCommand { get; set; }
// The event to fire
public event EventHandler<ShowMessageEventArgs> ShowMessage;
public MainWindowViewModel()
{
ButtonClickCommand = new RelayCommand(ButtonClicked);
}
void ButtonClicked(object param)
{
// This button is wired up in the view as normal and fires the event
OnShowMessage("You clicked the button");
}
// Fire the event - it's up to the view to decide how to implement this event and show a message
void OnShowMessage(string message)
{
if (ShowMessage != null) ShowMessage(this, new ShowMessageEventArgs(message));
}
}
public class ShowMessageEventArgs : EventArgs
{
public string Message { get; private set; }
public ShowMessageEventArgs(string message)
{
Message = message;
}
}
The XAML would be:
<Button Command="{Binding ButtonClickCommand}">Click me!</Button>
So the button invokes the command, which in turn fires the event which the view (MainWindow) handles and shows a messagebox. This way the view/UI decides on the course of action based on the type of event raised. Of course it could be your timer which fired the event
You can always go down the more involved route such as some of the answers on this question...
How should the ViewModel close the form?
but to be honest, it depends if you really need it - a simple event works well - some people overcomplicate things for the sake of elegance, but at the detriment of simplicity and productivity!
I would go this way:
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
// View
public partial class TestActivateWindow : Window
{
public TestActivateWindow() {
InitializeComponent();
Messenger.Default.Register<ActivateWindowMsg>(this, (msg) => Activate());
}
}
// View Model
public class MainViewModel: ViewModelBase
{
ICommand _activateChildWindowCommand;
public ICommand ActivateChildWindowCommand {
get {
return _activateChildWindowCommand?? (_activateChildWindowCommand = new RelayCommand(() => {
Messenger.Default.Send(new ActivateWindowMsg());
}));
}
}
}
public class ActivateWindowMsg
{
}

How to inform a UserControl that an event has occurred in another UserControl?

I have a Window called MainWindow which contains 2 UserControls:
SearchBox
and
CustomerList
When someone types into SearchBox I want to change CustomerList accordingly.
Question: What is the best way for CustomerList to know that the TextChanged event has been executed in SearchBox?
Right now I have SearchBox defined as:
public class SearchSubmittedEventArgs : EventArgs
{
public string Term { get; set; }
public SearchSubmittedEventArgs(string term)
{
Term = term;
}
}
public partial class SearchBox : UserControl
{
public event EventHandler<SearchSubmittedEventArgs> SearchSubmitted;
public SearchBox()
{
InitializeComponent();
}
private void SearchTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (SearchSubmitted != null) SearchSubmitted.Invoke(this, new SearchSubmittedEventArgs(((TextBox)sender).Text));
}
}
and MainWindow has the following XAML and EventHandler:
<UserControls:SearchBox SearchSubmitted="SearchSubmitted_Invoked" />
private void SearchSubmitted_Invoked(object sender, SearchSubmittedEventArgs e)
{
// I know SearchBox has been typed into
// but I don't know how best to inform CustomerList
}
Do I need to pass the event from SearchSubmitted_Invoked to CustomerList? Is there a way for me to pass it directly from SearchBox to CustomerList?
I think you can just subscribe to the event...Something along these lines:
var search = new SearchBox();
var cust = new CustomerList();
search.SearchSubmitted += (s,e)=>{ cust.Update(e.Term); }
Hope it helps you find a way to move along...
Are those controls near each other? Then it would maybe make sense to bake a new user control with them inside. That outer user control is the one that should do the event handling.

Categories

Resources