[WPF]How to close popup - c#

I'd like to close a popup automatically with controling StaysOpen value which is popup property. I've been opening it whenever text is entered or left mouse button is clicked.
StaysOpen is set to false.
<Grid>
<TextBox x:Name="textBox" PreviewMouseLeftButtonUp="textBox_PreviewMouseLeftButtonUp"/>
<Popup IsOpen="{Binding IsOpen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" OpacityMask="Transparent" StaysOpen="{Binding StaysOpen, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
AllowsTransparency="True" PlacementTarget="{Binding ElementName=textBox}">
<Border CornerRadius="5" Background="#FF303030" Width="{Binding ActualWidth, ElementName=textBox}">
...
</Border>
</Popup>
</Grid>
private void textBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if ((sender as TextBox).Text.Length > 0)
{
IsOpen = !IsOpen;
}
}
When text is entered in Textbox with Popup's IsOpen is true, can be closed automatically popup when clicked out side of the control. But, in a state of textBox_PreviewMouseLeftButtonUp function call with same situations, wouldn't be closed it automatically.(also IsOpen is true)
If i change to e.handel = true in textBox_PreviewMouseLeftButtonUp, then can be closed automatically, through clicking the outside of it. But it have serious problem on that, can not choose any of controls in the popup. so can not use this way.
How can I safely close the popup automatically by clicking out side of control?

You can use the TextBox's LostKeyboardFocus event, which fires when the TextBox is no longer the destination for keyboard input (i.e. when the user clicks or tabs away from the control).
private void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
IsOpen = false;
}
Also, for opening the popup, I might recommend using GotKeyboardFocus. PreviewMouseLeftButtonUp will only work if the user sets focus by left-clicking, but the user could also use tab, etc.

Not perfect, but I solved it.
I added MouseLeftButtonUpEvent instead of PreviewMouseLeftButtonUp.
textBox.AddHandler(MouseLeftButtonUpEvent,
new RoutedEventHandler(textBox_MouseLeftButtonUp),
true);
When external control is clicked, Pop up is closed automatically, and when TextBox is clicked, Pop up is opened.
However, If Pop up is displayed, When TextBox is clicked, Pop up isn't hidden.
Anyway, I think this is enough.

Related

WPF Popup Closing Animatiom

<Popup
AllowsTransparency="True"
Focusable="False"
PopupAnimation="Slide"
Width="{Binding ElementName=grid,Path=ActualWidth}"
Height ="{Binding ElementName=grid,Path=ActualHeight}"
Name="popup"
Placement="Relative"
PlacementTarget="{Binding grid}" >
</Popup>
This popup opens by slide animation perfectly when i set it's IsOpen Property to True. but why Popup closes immediately without any animation.
Is there a way to animate Popup closing ?
The current WPF implementation only supports the PopupAnimation.Fade animation for closing the popup.
You can check this in the source code:
if (animation == PopupAnimation.Fade)
{
_popupRoot.Value.SetupFadeAnimation(AnimationDelayTime, visible);
return true;
}
else if (visible) // only translate when showing popup
{
// translate the content
_popupRoot.Value.SetupTranslateAnimations(animation, AnimationDelayTime, AnimateFromRight, AnimateFromBottom);
return true;
}
For PopupAnimation.Fade, both the showing and the closing are animated. For other animation types, only the visible (showing) state is animated.
You cannot change this behavior. You could of course attach a custom animation to your content, but the popup will be closed immediately anyway, so you won't see it:
if (!animating)
_secHelper.HideWindow();
If you can, use adorners instead of popups. There, you can setup your animation as you need. Alternatively, use the fade animation for popups.

How button name as Backspace work as Backspace for multiple textboxes

On keyboard button name as BackSpace do not work as backspace for multiple textBoxes.
for single textbox i write this code and successfully .
textbox.text = textbox.Text.Remove(textbox.Textlength-1,1);
but not work for multiple textboxes.
Edit Note that this is an WPF answer but the question is about winforms. I'll leave the answer for now in case someone looks for the same thing with WPF.
You can use the FocusManager.IsFocusScope property:
<StackPanel>
<StackPanel x:Name="tbElements" FocusManager.IsFocusScope="True">
<TextBox x:Name="tb1" Margin="3" VerticalAlignment="Top"/>
<TextBox x:Name="tb2" Margin="3" VerticalAlignment="Top"/>
</StackPanel>
<Button Content="Test" Margin="3" Height="26" Click="Button_Click"/>
</StackPanel>
Then the textboxes are managed in a separate logical focus scope
private void Button_Click(object sender, RoutedEventArgs e)
{
var test1 = FocusManager.GetFocusedElement(this);
var test2 = FocusManager.GetFocusedElement(tbElements);
}
The result will be, that test1 references the clicked button, because it has the current focus, but test2 references the last focused textbox, because it has the local focus within its separate logical focus scope.
An alternative would be to set the Button property Focusable to False, so if a textbox has the focus when the button is clicked, the focus stays within the textbox. This prevents keyboard navigation to the button but since the button is part of the screen keyboard, this may be acceptable or even desired behavior.
I am pretty sure that OP does not want to perform backspaces on multiple textboxes, rather wants Backspace button to affect textbox currently focused, so:
To emulate a keyboard stroke use SendKeys.Send(string keys) instead of current implementation. This will perform BACKSPACE on control which has focus:
private void backspace_Click(object sender, EventArgs e)
{
//{BACKSPACE}, {BS}, or {BKSP}
SendKeys.Send("{BACKSPACE}");
}

win10 UWP OnScreen Keyboard is not hiding

I have created a Win 10 UWP application . in that I have a popup for which light dismissal is enabled.
<Popup x:Name="AddWebpagePopup" IsLightDismissEnabled="True" IsOpen="{Binding IsPopupOpen}" Opened="AddWebpagePopup_Opened">
<TextBox x:Name="WebpageNameTextBox" Text="{Binding WebpageUrl, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource TexBoxStyle}" KeyDown="WebpageNameTextBox_KeyDown" />`
<Button Content="Cancel" Command="{Binding CancelCommand}" HorizontalContentAlignment="Center"/>
</popup>
In AddWebpagePopup_Opened I am just setting the focus to WebpageNameTextBox. In CancelCommandI am just setting IsPopupOpen to False
My issue is with OnScreenKeyBoard In tablet mode. Keyboard is showing properly when the popup is opened and closing when cancel button is clicked. Issue is there only when I touch outside the popup. Popup was dismissed but the keyboard was still visible. Anyone have idea why this happen?
When someone presses the "Cancel" button, the button gets focus -> the keyboard hides. It seems that the closing of popup is not enough to lose focus, so the keyboard is still visible.
You can try to change the focus when the popup is being closed. Although there is no closing event, you can register to property changed callback event.
this.AddWebpagePopup.RegisterPropertyChangedCallback(Popup.IsOpenProperty, (d, e) =>
{
if (!this.popup.IsOpen)
{
// change focus to something else
}
});

Open popup when textbox has keyboard focus and close via "staysopen = false" property

As mentioned in the title, I want the popup to show, when the textbox gains keyboard focus, but then close when the user clicks outside of the popup (which is done via staysopen = false).
As you can see by the code below, I have bound the IsOpen property to the ExampeTextBox's IsKeyboardFocused property. This works for opening the popup, but prevents me from interacting with the popup, as I call Keyboard.ClearFocus(); in a click event that gets called when the user clicks anywhere on the window. This means that clicking anywhere outside of the textbox causes Keyboard.ClearFocus() to fire, causing the textbox to lose keyboard focus.
If I set StaysOpen = false and then set IsOpen programmatically via Popup.IsOpen = true; it causes the properties IsOpen and StaysOpen to conflict and the popup won't show up at all.
<Popup
x:Name="ExamplePopup"
Placement="Bottom"
PlacementTarget="{Binding ElementName=ExampleTextBox}"
IsOpen="{Binding ElementName=ExampleTextBox, Path=IsKeyboardFocused, Mode=OneWay}">...</Popup>
Here is a screen recording of my problem.
Any help is much appreciated thanks.
You can bind IsOpen to a boolean property which makes your popup visible or not.
<Popup
x:Name="ExamplePopup" StaysOpen="True"
Placement="Bottom"
PlacementTarget="{Binding ElementName=ExampleTextBox}"
IsOpen="{Binding IsNeedToOpen}">
</Popup>
When your textbox gets focus set the property value true and when you need close popup set the property value false. Make sure you implement INotifyPropertyChanged interface and set DataContext.
private bool _IsNeedToOpen = false;
public bool IsNeedToOpen
{
get { return _IsNeedToOpen; }
set
{
if (_IsNeedToOpen == value) { return; }
_IsNeedToOpen = value;
OnPropertyChanged("IsNeedToOpen");
}
}
private void TextBox_GotKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
{
IsNeedToOpen = true;
}

In Windows phone,How to change checkbox state after questioning user?

In my app,there is a need to do this
At start,the checkbox is unchecked
user tap,and then pop up a messagebox as a alarm to make sure user indeed want to do it(At the moment the checkmark is still collapse)
If user click "Yes,I want to do it",then checkmark is visible and now it is checked
vice versa
I found that,when I tap the checkbox,Checked event is always triggering
and
the checkmark is always turn to "checked" state
How to solve the problem???
Any advice would be great,Thanks!!!
Just a trick is needed. Sharing a sample with you.
overlap a transparent background grid over your checkbox with a transparent background like this.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Grid>
<CheckBox Name="cb" Content="cb" Checked="CheckBox_Checked_1"/>
<!--Grid that overlaps the checkbox-->
<Grid Background="Transparent" Tap="Grid_Tap_1"/>
</Grid>
</StackPanel>
</Grid>
This overlapping wont call any checkbox event even if you tap on it
now in code of the event
private void Grid_Tap_1(object sender, GestureEventArgs e)
{
if(MessageBox.Show("Message")==MessageBoxResult.Ok)
{
cb.IsChecked=True;
}
}
Instead of Tap Event, Try Checked and Unchecked Events of the checkbox.
Note: You can track the checked status in "Checked" and "UnChecked" events of the Check Box using IsChecked Property and write your code in appropriate events.
After Asking confirmation to user.
If the user clicks "Yes" , set chkBox.IsChecked=true;
else
If the user clicks "No", set chkBox.IsChecked=false;
I think there is no way to stop checkbox default behaviors. So you can create a custom control with a image and a textbolck inside a stack panel. You should use pair of images for "Image" Control Source one for unchecked and another for checked.
//At start,the checkbox is unchecked
<StackPanel x:Name="panelCheckBox" Tap="panelCheckBox_Tap_1" Orientation="Horizontal">
<Image Source="UncheckImageSourc" x:Name="ImgeCheckBox"/>
<TextBlock Text="CheckBox Content"/>
</StackPanel>
In Code Behind
bool IsChecked=false;
private void panelCheckBox_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
{
//If user click "Yes,I want to do it",then checkmark is visible and now it is checked
if(!IsChecked)
{
IsChecked =true;
ImgeCheckBox.Source = "CheckImageSource";
}
else
{
IsChecked =false;
ImgeCheckBox.Source = "UncheckImageSourc";
}
}

Categories

Resources