On my MenuItem at the moment I open up a Window. Then if the person chooses to click on that MenuItem again, I need the Window that was open to close.
Then obviously, if they click it the third time it will open and so fourth.
XAML
<MenuItem x:Name="btnHelp" Click="btnHelp_Click" Foreground="#FF7E8385" FontFamily="Calibri" FontSize="18" Margin="110,10,0,0" Height="30" Width="70" Style="{x:Null}" BorderBrush="Transparent" Background="Transparent" Cursor="Hand"/>
Code behind
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
xamlHelp help = new xamlHelp();
help.Show();
}
You'll need to have the variable be an instance field, rather than a local variable, so it can be accessed between calls. At that point just close it if it exists, and recreate/show it if it doesn't:
private xamlHelp help = null;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (help != null)
{
help.Close();
help = null;
}
else
{
help = new xamlHelp();
help.Show();
}
}
You can keep track of whether the help is currently showing and show/hide based on that,
private xamlHelp help = new xamlHelp();
private bool showingHelp = false;
private void btnHelp_Click(object sender, RoutedEventArgs e)
{
if (showingHelp)
{
help.Hide();
}
else
{
help.Show();
}
}
Related
Click event of Button is fired when pressing D while writing to Textbox.
Is there an elegant way how to suppress Keyboard Accelerator while Textbox is focused?
XAML:
<StackPanel Orientation="Vertical">
<TextBox></TextBox>
<Button Click="Button_Click" Content="Button with "D" as keyboard accelerator" Margin="0,10">
<Button.KeyboardAccelerators>
<KeyboardAccelerator Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
</Button>
<TextBlock x:Name="ButtonClickCounter"></TextBlock>
</StackPanel>
C#:
int buttonClickCounter;
private void Button_Click(object sender, RoutedEventArgs e)
{
ButtonClickCounter.Text = $"Button clicked {++buttonClickCounter} times";
}
EDIT:
Why Accelerator with Modifier (Alt+D or Ctrl+D) is not solution?
I am creating video player and I found that one-key shortcuts are neat solution for fast operations with video player (same as in VLC).
Best Solution so far:
Creating custom KeyboardAccelerator, that checks if focus is set to text box. Only edit in code that need to be done is changing KeyboardAccelerator to AcceleratorWithHandledActionIfTextboxIsFocused.
public class AcceleratorWithHandleDActionIfTextboxIsFocused:KeyboardAccelerator
{
public AcceleratorWithHandleActionIfTextboxIsFocused()
{
Invoked += AcceleratorWithHandleActionIfTextboxIsFocused_Invoked;
}
private void AcceleratorWithHandleActionIfTextboxIsFocused_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
{
var focusedElement = FocusManager.GetFocusedElement();
if (focusedElement.GetType() == typeof(TextBox))
args.Handled = true;
}
}
part of XAML:
<Button.KeyboardAccelerators>
<custom:AcceleratorWithHandleDActionIfTextboxIsFocused Key="D"></KeyboardAccelerator>
</Button.KeyboardAccelerators>
I agree with #Thomas Weller in his comments as for not using a single letter as a keyboard accelerator...
But everybody has their own requirements, anyway, you could try to e.handle = true your event when textbox is focused.
Something like this:
public void Button_Click(object sender, ExecutedRoutedEventArgs e)
{
if (textBox.isFocused)
{
e.handled = true;
}
}
I developed an app in Windows Phone 8. I have a list with mp3 from web and a mediaelement can't play them.
This is my code
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.MediaOpened += new RoutedEventHandler(note1_MediaOpened);
}
void note1_MediaOpened(object sender, RoutedEventArgs e)
{
play.Play();
}
And xaml is :
<MediaElement HorizontalAlignment="Left" Height="100" Margin="167,538,0,0" VerticalAlignment="Top" Width="100" Name="play" AutoPlay="True"/>
I have try it in xaml and play it but programmatically not. Can someone help me ?
I do not think you need to Play inside MediaOpened event. Just execute .Play after assigning Source to the MediaPlayer
Just add this to button click event
private void Button_Click_1(object sender, RoutedEventArgs e)
{
play.Source = new Uri("http://n1.xtek.gr/ime_ploigos/rest/narrations/gr/10.mp3",UriKind.RelativeOrAbsolute);
play.Play();
}
Im using Mousebindings in my view to listen to user clicks like this:
<Path.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleLeftClickProj}" />
<MouseBinding Gesture="LeftClick" Command="{Binding SingleLeftClick}"/>
</Path.InputBindings>
I only need one of the mouse gestures at a time. So if I double click on my application I want to ignore the single left click mousebinding. Possibly like waiting 1-2sec after the initial mouseclick then decided which should be called. Is there a simple way of doing this?
I got it working the following way (I used a Button to test it, you'll have to adapt it).
Use Event Handlers
<Button MouseDoubleClick="Button_MouseDoubleClick" Click="Button_Click"></Button>
store the DataContext in a static variable
private static object context;
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
context = DataContext;
}
Adapt this code (I mainly got it from https://stackoverflow.com/a/971676/4792869)
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 1),
DispatcherPriority.Background,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher);
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
((ICommand)DataContext).Execute("DoubleLeftClickProj");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
((ICommand)context).Execute("SingleLeftClick");
}
In my WPF application I have Toggle Button, I want to detect when user double click on it (in both cases if it checked or unchecked).
How can I do that?
Thanks in advance
You can use the OnPreviewMouseDoubleClick event
xaml:
<ToggleButton Height="75" Width="100" PreviewMouseDoubleClick="Control_OnPreviewMouseDoubleClick"/>
code-behind:
private void Control_OnPreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var toggleButtton = sender as ToggleButton;
if (toggleButtton != null)
{
if (toggleButtton.IsChecked.HasValue)
{
if (toggleButtton.IsChecked.Value)
{
// Checked
}
else
{
// Unchecked
// this will re-check the button if double-click unchecks it
//
toggleButtton.IsChecked = true;
toggleButtton.Focus();
}
}
}
}
Use PreviewMouseDoubleClick event (msdn):
XAML:
<ToggleButton x:Name="tButton" Height="30" Content="MyButton"
PreviewMouseDoubleClick="tButton_PreviewMouseDoubleClick"
/>
Code-behind:
private void tButton_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
tButton.IsChecked = !tButton.IsChecked.Value;
e.Handled = true;
//...
}
This event is pretty easy with togglebuttons
Xaml you write the following to get an EventHandler:
<ToggleButton Name="button1" MouseDoubleClick="button1_DoubleClick" />
In c# you write the following to get an EventHandler:
button1.MouseDoubleClick += new MouseButtonEventHandler(button1_DoubleClick);
And in both cases you need:
void button1_DoubleClick(object sender, MouseButtonEventArgs e)
{
}
Sorry i'm new in C# and i'm looking everywhere, and can't find even it looks easy to do.
I want to get the object by click on it but i don't know how to do it.
A simple button in xaml :
<TextBlock
Text="{Binding ProjectName}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
And i Use a simple function :
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(NoteFolders), MyProjects[3]);
}
But i would like to have the specific project like MyProjects[x] x=(click Project).
Any ideas ?
Try this
var element = (sender as FrameworkElement);
if (element != null)
{
var project = element.DataContext as Project;
if (project != null)
{
//Implementation
}
}
You need to cast the event OriginalSource to your type (e.g. Project):
this.Frame.Navigate(typeof(NoteFolders), (Project) e.OriginalSource);
You can store project id in a tag property of a control
<TextBlock
Text="{Binding ProjectName}" Tag="{Binding ProjectID}"
VerticalAlignment="Center" Tapped="On_Tapped_Project"/>
You can recover it by access to a tag property
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = ((TextBlock)sender).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
is a way to do it, but it is by far more recommended to do it this way:
private void On_Tapped_Project(object sender, TappedRoutedEventArgs e)
{
var projectID = (sender as TextBlock).Tag as int;
Frame.Navigate(typeof(NoteFolders), MyProjects[projectID]);
}
"sender as ElementType" is the correct way to cast sender, other methods can cause minor issues, always do the best code and never wonder why it went wrong that one time.