Mouse Click in Silverlight 4 - silverlight

I need a mouse click event in my silver light project and I know we need to simulate it ourself if the object is not button. lets say I want mouseclick for my img...
How exactly can we track time between mousedown and mouseup and say if the time between them is less than 300m, we have a mouse click?

Handle the MouseLeftButtonDown and MouseLeftButtonUp events for your image.
private DateTime? startClick;
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startClick = DateTime.Now;
}
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var clickDuration = DateTime.Now - startClick;
if (startClick != null && clickDuration < TimeSpan.FromMilliseconds(300))
{
MessageBox.Show("Less than 300ms!");
}
startClick = null;
}

Related

Programatically fire Treview item selction event in wpf

I'm working on WPF project where I populate a tree view (MainWindow.xaml)using a Class called SampleTreeItem (SampleTreeView class). This tree view shows files from the selected folder. when user clicks the tree item (mainWindow.cs), the code in the background opens the file, serializes it and extract the data from it.
Now I'm adding a checkbox to this tree through the class using header template.
There is event called OnItemSelected (in Mainwindow.cs) which gets fired when I select item from tree view.
I'm trying to handle the click event of the checkbox in Sampletree.cs and I want to fire OnItemSelected from mainwindow.cs when user clicks on the check box.
MainWindow.xaml.cs
private void filesTreeView_OnItemSelected(object sender, RoutedEventArgs e)
{
SampleFileTreeItem treeViewItem = e.Source as SampleFileTreeItem;
if (treeViewItem == null)
return;
OpenSampleFile(treeViewItem.FilePath);
}
This is the click event in the SampleTree.cs file.
public void CompareSamples_Click(object sender, RoutedEventArgs e)
{
//RoutedEvent routedEvent = e.RoutedEvent;
//e.Handled= true;
//e.Source = this;
//RoutedEventArgs args = new RoutedEventArgs(routedEvent, e);
RoutedEvent routedEvent = TreeView.SelectedItemChangedEvent;
e.Source = this;
RoutedEventArgs args = new RoutedEventArgs(routedEvent, e);
if (compareSamplesCheckbox.IsChecked == true)
{
OnSelected(args);
}
else
{
//object sender, RoutedEventArgs e
}
}
I don't know what I'm missing here but I cant fire OnItemSelected (mainWinodw) from SampleTree.cs.
Let me know if anyone has any thoughts on it. Appreciate any help.
I assume, that OnSelected(..) is an event handler. So in your code you need not to call an event handler, but to raise an event for the UIElement:
public void CompareSamples_Click(object sender, RoutedEventArgs e)
{
RoutedEvent routedEvent = TreeView.SelectedItemChangedEvent;
e.Source = this;
RoutedPropertyChangedEventArgs<object> args = new RoutedPropertyChangedEventArgs<object>(oldVal, newVal, routedEvent);
if (compareSamplesCheckbox.IsChecked == true)
{
yourTreeView.RaiseEvent(args);
}
else
{
//object sender, RoutedEventArgs e
}
}

DatePickerSelectedDateChanged event occurs twice when manually enter date

I have a DatePicker managing SelectedDateChanged event:
<DatePicker Name="myDatePicker" SelectedDateChanged="myDatePicker_SelectedDateChanged" />
If I choose a date with the calendar, SelectedDateChanged event is fired once. If I change date manually, SelectedDateChanged event is fired twice.
I found other people speaking about the same problem and resolving it by setting a flag when event is first fired, and test the flag value.
I would like to find a different approach to solve my issue, without using a flag.
First time it fires for DatePicker SelectedDate and second time for Calendar SelectedDate... So it's not error, it's strange logic.
I managed to do what I wanted.
When form is loaded, I added GotFocus and LostFocus events to the DatePicker TextBox (using Template.FindName method). I also added CalendarOpened and CalendarClosed event on the DatePicker.
It's not very elegant and if you think there is a better way, fell free to comment, I am a beginner in WPF...
public partial class MainWindow : Window
{
private TextBox txtDtPicker;
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtDtPicker = (TextBox)myDatePicker.Template.FindName("PART_TextBox", myDatePicker);
myDatePicker.CalendarOpened += new RoutedEventHandler(myDatePicker_CalendarOpened);
myDatePicker.CalendarClosed += new RoutedEventHandler(myDatePicker_CalendarClosed);
txtDtPicker.GotFocus += new RoutedEventHandler(txtDtPicker_GotFocus);
txtDtPicker.LostFocus += new RoutedEventHandler(txtDtPicker_LostFocus);
}
private void txtDtPicker_GotFocus(object sender, RoutedEventArgs e)
{
myDatePicker.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(myDatePicker_SelectedDateChanged);
}
private void txtDtPicker_LostFocus(object sender, RoutedEventArgs e)
{
myDatePicker.SelectedDateChanged -= new EventHandler<SelectionChangedEventArgs>(myDatePicker_SelectedDateChanged);
}
private void myDatePicker_CalendarOpened(object sender, RoutedEventArgs e)
{
myDatePicker.SelectedDateChanged += new EventHandler<SelectionChangedEventArgs>(myDatePicker_SelectedDateChanged);
}
private void myDatePicker_CalendarClosed(object sender, RoutedEventArgs e)
{
myDatePicker.SelectedDateChanged -= new EventHandler<SelectionChangedEventArgs>(myDatePicker_SelectedDateChanged);
// When calendar is closed after a date selection, unfocus and focus again txtDtPicker. If not done, SelectedDateChanged event will not fire if date is changed manually just after changed was done with calendar
myDatePicker.Focus();
txtDtPicker.Focus();
}
int iNbTimesEventOccurs = 0;
private void myDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
// Remove event to prevent it to happen twice
myDatePicker.SelectedDateChanged -= new EventHandler<SelectionChangedEventArgs>(myDatePicker_SelectedDateChanged);
// Just to control event is not fired several times
iNbTimesEventOccurs++;
textBlock1.Text = "Nb times event occurs: " + iNbTimesEventOccurs;
}
}

Item Right Tapped in ListView WPF

Is there a way to get OnItemRightTapped event on ListView or GridView that works exactly like ItemClick, except obviously react only on right tap?
You can add an event handler for mouse down and then determine the click source in the code:
private void listView_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Right)
{
// Do what u need to do here
}
else
e.Handled = true;
}

TextBox (PasswordBox) SelectAll method doesn't work

I have some concept question here. I know how to select all text in TextBox or in PasswordBox. Via GotKeyboardFocus and PreviewMouseLeftButtonDown events, you know. This works fine.
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
GotKeyboardFocus="SelectAllPassword"
CodeBehind
private void SelectAllPassword(Object sender, RoutedEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
pb.SelectAll();
}
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
var pb = (sender as PasswordBox);
if (pb != null)
if (!pb.IsKeyboardFocusWithin)
{
e.Handled = true;
pb.Focus();
}
}
But question is - why this doesn't work?
XAML:
PreviewMouseLeftButtonDown="PasswordOnPreviewMouseDown"
CodeBehind:
private void PasswordOnPreviewMouseDown(Object sender, MouseButtonEventArgs e)
{
_txtPassword.SelectAll();
e.Handled = true;
}
Where _txtPassword - TextBox or PasswordBox control. So why I'm enforsed to Focus text control?
Actually, the selection is working.
You may feel that the text is not selected because it's not visually highlighted, but that's because the TextBox is not focused.
Try giving focus to your TextBox with the Tab key, you'll see the whole text highlighted.

Wpf, I need to hide and show a groupbox?

How I can show or hide a groupbox from xaml.cs. I try to do this in the checkbox's event:
private void cbDaily_Checked(object sender, RoutedEventArgs e)
{
gbCalendar.Visibility = Visibility.Visible;
}
But this don't work.
It must be work on checked/unchecked events of checkbox like this way :
private void chkTest_Checked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Visible;
}
private void chkTest_Unchecked(object sender, RoutedEventArgs e)
{
grpTest.Visibility = System.Windows.Visibility.Hidden;
}
It is working fine in my sample application. Can you please give more details of your problem, so I can have better idea. Is event fired properly ? Make sure name of groupbox in code behind is correct or not ?

Resources