I'm pretty new to silverlight so, i'm having this problem about communication between user controls. I have user controls that have buttons in them which are supposed to set some properties of other user controls. For example, IsEnabled property to be set as true or false or visibility, and so on. I actually know one solution which would be something like:
class UserControl1 : Usercontrol
{
public UserControl2 uc2;
private void Button1_Click(object sender, RoutedEventArgs e)
{
uc2.IsEnabled=False; // or uc2.SomeMethod();
}
}
Similar goes for UserControl2 class, and then in main page i only add:
UserControl1.uc2=UserControl2;
My questions is, how can i do this via Event Handlers? Or maybe there is some othe better solution? A simple example would be welcome. Thanks.
Another approach is to use a Event Aggregator for such communication. We in our project are using Prism's event aggregator. Please check the following thread.
Things to keep in mind while using event aggregator are
Keep their usage to minimal. This is because event subscriptions using event aggregator may be difficult to debug. So within the same class use normal events.
Name the events when using event aggregator in a way that describes the event. For example if you click a save button to save customer, use a event name such as BeforeCustomerSave\CustomerSaved instead of SaveButtonClicked.
Related
I was searching all over the place but I couldn't find an answer. I need to fire up an ListView item action, so it would rise the ItemActivate event. For now, it's only possible using ENTER key or double click... I would like to know if I could programmatically do that, something like :
listView.Items[int].Activate();
This doesn't work of course, because that function Activate() is not implemented there. For example, I couldn't find how to trigger buttons programmatically, but there it was, in the context menu which appears while you type:
buttonX.PerformClick();
...and it would trigger the button_click event. I wonder if there's something similar in the ListView control for triggering items inside of it ? I want it to raise this event programmatically and not by mouse doubleclick or Enter key on the keyboard...
private void myListView_ItemActivate(object sender, EventArgs e)
{
MessageBox.Show("hello");
}
According to the documentation, the EventHandler in the ListView should be public. So you can raise the event with:
myListView.ItemActivate(myListView, EventArgs.Empty);
This way everyone who subscribed to this event will get notified.
Another way, of course, is to directly call your method:
myListView_ItemActivate(this, EventArgs.Empty);
But this doesn't really classify as "raising the event", because you actually don't raise an event. You just call a method.
I have 2 Tab controls (UserControl) in my WPF application. Since both this tab controls are created dynamically based on back end data, I have both the tab controls of same class type but different objects (confirmed it by checking the memory address of both, which are different)
I have this strange situation where Unload event is not getting fired when I switch between the tabs (UserControl) of same type. However, the Unload event gets called when I switch from/to tabs of different type.
Am I suppose to handle some other event apart from Unload event?
Please help me with this.. I am stuck :(
After spending some more time and investigating, I could come to the solution to my question.
The issue was seen because WPF was just changing the DataContext of my UserControl everytime I switch between the Tab controls of same type and since the UsrControl was of same type, even though different object, Unload event was not getting triggered.
The solution to this problem is instead of handling Unload event, handle DataContextChanged event of the UserControl.
Thanks all for the answers !!
Cheers,
Nayan
For this you need to subscribe to the OnSelectionChanged event, like so:
XAML:
<TabControl SelectionChanged="TabControl1_OnSelectionChanged">
Code-behind:
private void TabControl1_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//do whatever
}
maybe my design is not good, or I don't see the obvious solution, but I want to subscribe to a buttonClick EventHandler of Form1 from outside form1.
For example I have a Controller and Form1 who are both instanced in the main function.
Now I want to subscribe a function from Controller to the buttonClick event from Button1_Click in Form1. But the button1 is declarded private, so i can't do
form1->Button1->Click += gcnew EventHandler(controller->function)
Is there any way to get around this?
Ok I could write a setter or something in Form1, but is there any other solution?
I read some examples, but they are all calling events from within the same class so they don't address my specific problem.
EDIT
Maybe it helps if I say what I really want to achieve:
Ok there is the GUI aka Form1 and a Conroller Class.
The Controller should get notified, if the user triggers a specific ButtonClick event on the GUI.
Also the Controller should be able to subscribe and unsubscripe from different events during runtime. To make it even more confusing (atleast for me) the controller should raise events, which trigger some GUI behaviors, like enabling some buttons and disabling others.
So this is what I want to do, at least in theory it sounded good, but now I have problems with the implementation.
This is something you ought to refactor of course. Add an event to the Form1 class and let the button1's Click event raise the event.
Assuming this is difficult: there's a back-door through the public Controls property:
form1->Controls["Button1"]->Click += // etc...
I’m building a WPF application using MVVM pattern (both are new technologies for me). I use user controls for simple bits of reusable functionality that doesn’t contain business logic, and MVVM pattern to build application logic. Suppose a view contains my user control that fires events, and I want to add an event handler to that event. That event handler should be in the view model of the view, because it contains business logic. The question is – view and the view model are connected only by binding; how do I connect an event handler using binding? Is it even possible (I suspect not)? If not – how should I handle events from a control in the view model? Maybe I should use commands or INotifyPropertyChanged?
Generally speaking, it is a good MVVM-practice to avoid code in code behind, as would be the case if you use events in your user controls. So when possible, use INotifyPropertyChanged and ICommand.
With that said, depending on your project and how pragmatic you are, some times it makes more sense to use the control's code behind.
I have at a few occasions used something like this:
private void textBox1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MyViewModel vm = this.DataContext as MyViewModel;
vm.MethodToExecute(...);
}
You could also consider Attached Command Behaviour, more info about this and implementations to find here:
Firing a double click event from a WPF ListView item using MVVM
I have been working on a WPF application and I am using the ModelViewViewModel design pattern. I have a number of events that come out of the view, that result in ViewModel activity.
What is the resonable way to get these events raised from a UnitTest? For example, I want to simulate the drop event. I don't really want to build a stub view, just to raise the event.
Any suggestions welcome.
Thanks.
According to the MVVM pattern:
The View knows about the ViewModel - it will have a reference to it either as a concrete instance or an interface
The ViewModel should not know about the view at all.
If you need to handle events, then there are two ways which I know of to do it:
1: Expose a command in your viewmodel, and use databinding to trigger it. This is my preferred way, eg:
class MyViewModel
{
public ICommand ClickCommand { get; set; }
}
<Button Command="{Binding Path=ClickCommand}" />
If you do this then you can test the command by simply calling myViewModel.ClickCommand.Execute manually.
2: Expose a function in the viewmodel, and write the absolute minimum in the .xaml.cs file to handle the event and call the function, eg:
class MyViewModel
{
public void HandleClick(){ }
}
<Button Click="MyClickHandler">
//.xaml.cs file
public void MyClickHandler( Object sender, EventArgs e ) {
m_viewModel.HandleClick()
}
If you do this, then you can test by simply calling myViewModel.HandleClick manually. You shouldn't need to bother with unit testing the MyClickHandler code as it's only 1 line!
It sounds like you have an event handler for the drop event directly in your ViewModel class. Would it make more sense to have the handler in your UI layer, which in turn will call a function in your ViewModel? This way, your unit test could just call the function (simulating a drag and drop operation, as far as the ViewModel is concerned).
Plus, it would better separate your ViewModel from your UI code.
Don't raise an event, just call the handlers (which means they should be public, and probably take less event handler centric arguments). Check out how this is done in Caliburn (http://www.codeplex.com/caliburn) using "Actions".
Why don't you use a mocking framework, such as Moq? Check out their quick start, it has a sample on mocking events. The url is: http://code.google.com/p/moq/wiki/QuickStart