ComboBox Event not Comming - silverlight

I am using silverlight. I have problem that added "ComboBox" on simple Silverlight Page. But there is no event for ComboBox.

My guess is that you've added a ComboBox element in XAML, and are looking for a SelectionChanged event in code behind.
If so, try this:
Add the SelectionChanged attribute to your ComboBox like this:
<ComboBox SelectionChanged="ComboBox_SelectionChanged">
</ComboBox>
Then go into your codebehind (F7 if you are in XAML view) and add the following inside your page class:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
That should get you started. Past that I would take some time to go through a few beginner complete Silverlight tutorials -- this should help familiarize you with both visual studio and the structure of a Silverlgiht app.

Related

silverlight keydown event doesn't work

I use VS2010, C# to develop Silverlight 4 app, I use following code in my XAML file:
<Canvas x:Name="Scene" FlowDirection="LeftToRight" Background="White" KeyDown="Scene_KeyDown" MouseMove="Scene_MouseMove">
and this is my XAML.cs file, I want to display a message box if any key is pressed (it is for test only):
private void Scene_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("1");
}
but nothing is displayed after keys are pressed! what is going wrong? should I set any property? command? tabstop? keypreview?
please help me
Looks like this answer will solve your problem:
You need to have at least something inside the Canvas that can receive
focus, and you will find that the event will bubble up.

SL4: need to register for a move (or redraw) event on an Item in an ItemsControl

Not finding a move event or redraw event in the FrameworkElement class. And Google not helping either. So...
I have a custom ItemsControl populated by an observable collection in the VM. The ItemsControl itself leverages the
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
behavior so the user can drag around the whole assembly.
When the user moves the assembly, I want to be notified by each item as the item is repositioned as a result of the assembly moving. So far I have tried registering for
this.myItem.LayoutUpdated += this.OnSomethingNeedsToUpdate;
but it doesn't seem to fire as I drag the assembly around.
Also
this.myItem.MouseMove += this.OnSomethingNeedsToUpdate;
only works if I mouse into the item which is not good enough. Because I am moving the ItemsControl and then have to go mouse into the item to get the event to fire.
Any ideas? Can I look to some ancestor in the visual tree for help in the form of a OneOfMyDecendantsWasRedrawn event or similar? Again I am trying to be notified when an item moves not be notified when the assembly moves.
I would say your best bet would be to add the MouseDragElementBehavior to your custom ItemsControl in code instead of in the Xaml. Here is how this might look (using a Grid since that is easier to demo):
public class DraggableGrid : Grid
{
public DraggableGrid()
{
Loaded += new RoutedEventHandler(DraggableGrid_Loaded);
}
void DraggableGrid_Loaded(object sender, RoutedEventArgs e)
{
MouseDragElementBehavior dragable = new MouseDragElementBehavior();
Interaction.GetBehaviors(this).Add(dragable);
dragable.Dragging += new MouseEventHandler(dragable_Dragging);
}
void dragable_Dragging(object sender, MouseEventArgs e)
{
// Custom Code Here
}
}
In the section that says Custom Code Here you would loop through you Items and notify them that they are being dragged.
I ended up writting another behavior for the individual items I care about and then wrote a LINQ query to search up the visual tree looking for ancestors with the MouseDragElementBehavior attached to them. That query found the ItemsControl since it was an eventual parent of the Item. I was then able to register for the Dragging event as desried.
Thanks again to Bryant for providing the solution over here.

Usercontrol with treeview selected event that refreshed another usercontrol not working

I'm relatively new to WPF programming and can't seem to get this working correctly. This is in my mind something rather simplistic that i would like to get working so I didn't try the MVVM framework and decided to keep it simple.
I have a usercontrol treeview with report paths. I have another usercontrol with a reportviewer. Selecting a report path in the treeview should refresh the reportviewer.
I can "call" the method from the treeview usercontrol for refreshing the reportviewer in the report usercontrol but nothing happens.
I was reading other posts and some seem to suggest a button event to a delegate that will allow the other usercontrol method to be executed. I'm not using a button event so I'm a bit puzzled.
I thought I could do something like this:
private void treeViewMenu_Selected(object sender, RoutedEventArgs e)
{
//Get the selected item
....
//Call the method of another user control
ReportUserControl ruc = new ReportUserControl();
ruc.RefreshReport();
}
Obviously that didn't work...I know that it shouldn't be too difficult but i am in a brain freeze moment and can't seem to get it working.
Any suggestions would be greatly appreciated.
Thanks!

wpf button click event

In this question I asked about adding TabItems dynamically to a TabControl
the ItemsSource are from ObservableCollection<Village>..
My question is if added a button to any TabItem, this button will have the DataContext of its container TabItem, how could I implement the Click event for this button?
If you have added the Button to the DataTemplate, then on your Button_Click method you can easily get the 'Village' datacontext.
void Button_Click(object sender, RoutedEventArgs e)
{
Village clickedVillage = ((Button)sender).DataContext as Village;
//Do whatever you want to do with the Village
}
But again, the above solution is not the best way to go for this problem. MVVM pattern would expect a ICommand in your Village (Or its container class) and you will bind that command to the Button.Command property so there wont be any code-behind at all. Or in other words your XAML will be more cleaner and ViewModel will get more self-contained in terms of properties and actions.

Print Button inside DataTemplate? How to write code

I have a ContentControl element whose ContentTemplate is determined at run time from the Resource Dictionary. In the datatemplate I have a visual (Convas) and what I want is to also have a button in datatemplate which when clicked should print the visual element(canvas).
As I said that the DateTemplate is inside the Resource Dictionary so How can I write a Code on Click Event of that button and where it should be?
Any response would be much appreciated.
Sounds like you can use the Button.Click attached event. Just add it to the ContentControl.
<ContentControl
Button.Click="Button_Click"
ContentTemplate="<template with a button>"
/>
and the handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
}
If you have multiple buttons in your template, you can use e.Source to figure it out. And I think you can use MouseButtonEventArgs instead.

Resources