I have a usercontrol ROOT_CONTROL that contains another user control SUB_CONTROL , which contains button BUTTON.
ROOT_CONTROL- > SUB -> BUTTON
<UserControl x:Name="ROOT_CONTROL"
GotKeyboardFocus="UserControl_GotKeyboardFocus">
<StackPanel>
<UserControl x:Name="SUB_CONTROL">
<Button x:Name="BUTTON" Click="BUTTON_Click">
</UserControl>
</StackPanel>
<SUB_Control>
</UserControl>
I have a UserControl_GotKeyboardFocus eventhandler connected with ROOT_CONTROL, I have an Button_Click eventhandler connected with BUTTON. When user clicks on BUTTON and there is a KeyboardFocus on that ROOT_CONTROL - it works ok, but when there is no KeyboardFocus on that control (or any of its children) it fires UserControl_GotKeyboardFocus (and that's ok), but then it does not fire BUTTON click event.
So when there is no keyboard focus on that control, user needs to click twice to see result (once to give focus, second to fire button event).
I do not change property e.Handled to true inside UserControl_GotKeyboardFocus eventhandler.
How to easily (if it is possible) fire BUTTON click eventhandler when there is no keyboardfocus on ROOT_CONTROL?
EDIT:
private void UserControl_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (Guard)
{
Guard = false;
}
else
{
if (e.OldFocus == null)
{
if (SessionData.SelectionWindow != null && !(SessionData.SelectionWindow.Visibility == Visibility.Visible))
{
SessionData.SelectionWindow.Ano.UpdateSelection();
SessionData.SelectionWindow.UpdatePosition();
SessionData.SelectionWindow.Visibility = Visibility.Visible;
if (e.OriginalSource is FrameworkElement)
{
(e.OriginalSource as FrameworkElement).Focus();
}
}
else
{
}
}
else
{
}
}
}
Related
So i have the TextBox:
<TextBox Controls:TextBoxHelper.ClearTextButton="True"
LostFocus="textboxNewValueCell_LostFocus"
TextChanged="textboxNewValueCell_TextChanged"/>
And when press on Clear button i want to catch the event.
Is it possible ?
I did not find any event
The ClearTextButton simply calls Clear() on the TextBox. There is no specific event raised. The best you can do is to handle the TextChanged event:
private void textboxNewValueCell_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Text.Length == 0)
{
//the TextBox was cleared and the Button was maybe clicked...
}
}
First, give your TextBox a name. Then, create a click event on the Button. when the click event fires, handle the clearing of the TextBox in the CodeBehind.
<Grid>
<TextBox x:Name="MyTextBox" Text="Some Text"/>
<Button x:Name="ClearButton" Click="ClearButton_Click"/>
</Grid>
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
MyTextBox.Text = string.Empty;
}
I defined a SplitButton in WPF(C#) with a binded item source.
The item list will be expanded only when the user clicks on the arrow at the right of the SplitButton.
How to expand the list when the user clicks on the SplitButton area?
I tried to handle the click event and set the property IsExpanded=true, but it automatically disappears after one second.
<Controls:SplitButton Name="SplitButton_Test"
Width="100"
HorizontalAlignment="Left"
HorizontalContentAlignment="Left"
ItemsSource="{Binding Dictionary_Test}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
Click="Test_Click">
<Controls:SplitButton.Icon>
<iconPacks:PackIconMaterial Margin="6" Kind="Alert" />
</Controls:SplitButton.Icon>
</Controls:SplitButton>
private void Test_Click(object sender, RoutedEventArgs e)
{
if (SplitButton_Test.IsExpanded == false)
{
e.Handled = true;
SplitButton_Test.IsExpanded = true; //Doesn't work, closes automatically after 1 second
}
}
This code in MahApps is closing it:
//Make popup close even if no selectionchanged event fired (case when user select the save item as before)
void ListBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var item = ContainerFromElement(_listBox, e.OriginalSource as DependencyObject) as ListBoxItem;
if (item != null)
{
IsExpanded = false;
}
}
currently at https://github.com/MahApps/MahApps.Metro/blob/develop/src/MahApps.Metro/MahApps.Metro.Shared/Controls/SplitButton.cs#L339
You need to remove that event handler. How? That's whole other question.
I have a dialog with few controls. There is a TextBox named txtControl and two Buttons Accept and Cancel. I want that once the focus is in txtControl, the focus should not go away, until I click on Accept or Cancel button.
If I try to click on any other control without clicking on Accept or Cancel button, then focus should remains in txtControl. Also I don't want to disable or gray out other controls.
You might handle OnPreviewMouseDown in the root, whenever focus is on txtControl, and the mouse is not over txtControl, Accept or Cancel;
void mainWindow_previewMouseDown(object sender, MouseEventArg e)
{
if (txtControl.IsFocusWithin)
{
if (txtControl.IsMouseOver == false ||
accept.IsMouseOver ==false ||
cancel.IsMouseOver ==false)
{
e.Handle = true;
}
}
}
and you might also hadle PreviewKeyDown to see if Tab is Pressed or not.
I would create an attached property that looked for the textbox losing Keyboad focus and just force focus back in to the textbox again.
The attached property would be something like this.
public class TextBoxExtras : DependencyObject
{
public static bool GetRetainsFocus(DependencyObject obj)
{
return (bool)obj.GetValue(RetainsFocusProperty);
}
public static void SetRetainsFocus(DependencyObject obj, bool value)
{
obj.SetValue(RetainsFocusProperty, value);
}
public static readonly DependencyProperty RetainsFocusProperty =
DependencyProperty.RegisterAttached("RetainsFocus", typeof(bool), typeof(TextBoxExtras), new PropertyMetadata(false, new PropertyChangedCallback((s, e) =>
{
TextBox textBox = s as TextBox;
if (textBox != null)
{
if (!(bool)e.NewValue && (bool)e.OldValue)
textBox.LostKeyboardFocus -= textBox_LostKeyboardFocus;
if ((bool)e.NewValue)
{
textBox.LostKeyboardFocus += textBox_LostKeyboardFocus;
textBox.Unloaded += textBox_Unloaded;
}
}
})));
static void textBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null )
if (textBox.Focusable)
textBox.Focus();
}
static void textBox_Unloaded(object sender, RoutedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
textBox.LostKeyboardFocus -= textBox_LostKeyboardFocus;
textBox.Unloaded -= textBox_Unloaded;
}
}
}
And use it in XAML like this, the first textbox is the one that will "retain focus"
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
Background="Black"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox local:TextBoxExtras.RetainsFocus="True" Margin="10,10,387,283"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,37,0,260" Width="120" />
<Button Content="Accept" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top" Width="75" />
</Grid>
</Window>
This kind of restriction is not a good idea.
How will your app be used by someone who can't use a mouse and uses the tab key to move between controls?
You could handle the PreviewLostKeyboardFocus at root level.
In xaml
<Window ... PreviewLostKeyboardFocus="Win_PreviewLostKeyboardFocus">
In C#
private void Win_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
// change focus behavior only when txtControl
// is the element losing focus
if (e.OldFocus != txtControl)
return;
// if new element with focus is not Accept and is not Cancel, then disable the focus change
if (e.NewFocus != Accept && e.NewFocus != Cancel)
e.Handled = true;
}
I have a list of toggle-buttons in wpf and I want the user to be able to toggle several buttons by dragging over them. To do this, I used the MouseEnter-Event for each button. This does work, when I press the mousebutton outside the buttons and start dragging. But when I press the mousebutton on a button and start dragging, the MouseEnter-Event is only fired for the first button, where I pressed the mousebutton (also none of the other events like mouseover or mousemove are fired).
Here's the code:
public void AddButton()
{
ToggleButton btn = new ToggleButton();
btn.MouseEnter += VisibilityButton_Enter;
this.gridButtons.Children.Add(btn);
}
private void VisibilityButton_Enter(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed)
{
ToggleButton btn = sender as ToggleButton;
btn.IsChecked = !btn.IsChecked;
}
}
I found a solution to use "drag and drop" and the dragover-event, but I think there must be an easier solution?
As Kent mentioned, the ToggleButton captures the mouse. If we handle the PreviewMouseDown event ourselves we can prevent that. The rest is just keeping track of the mouse state so the we don't click twice during a single roll-over. Here is a behavior you can add to your button to enable roll-over clicking.
First add this namespace:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
and the corresponding reference to your project.
Then the XAML looks like this (notice the RollOverBehavior):
<Grid>
<ItemsControl>
<ItemsControl.ItemsSource>
<PointCollection>
<Point/>
<Point/>
<Point/>
<Point/>
<Point/>
</PointCollection>
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton Width="25" Height="25">
<i:Interaction.Behaviors>
<local:RollOverBehavior/>
</i:Interaction.Behaviors>
</ToggleButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
and here is the behavior itself:
public class RollOverBehavior : Behavior<ToggleButton>
{
bool mouseOver;
bool clicked;
protected override void OnAttached()
{
AssociatedObject.PreviewMouseLeftButtonDown += (s, e) =>
{
AssociatedObject.IsChecked = !AssociatedObject.IsChecked;
e.Handled = true;
};
AssociatedObject.MouseEnter += (s, e) =>
{
mouseOver = true;
clicked = false;
};
AssociatedObject.MouseLeave += (s, e) =>
{
mouseOver = false;
};
AssociatedObject.MouseMove += (s, e) =>
{
if (mouseOver && !clicked && e.LeftButton == MouseButtonState.Pressed)
{
AssociatedObject.IsChecked = !AssociatedObject.IsChecked;
clicked = true;
}
};
}
}
The problem is that the default behavior of the ToggleButton is to capture the mouse when the left mouse button is clicked. Because the mouse is captured, all mouse events are sent to the first ToggleButton.
Sounds like what you want to do is override this default behavior such that the mouse isn't captured, but to be honest I couldn't really follow exactly what it is you're trying to achieve.
I had the same problem with normal Buttons. The solution, that worked for me, is to set e.Handled = true in the PreviewMouseButtonDown event (I implemented this too). It seems, that just by down-click with the mouse, the previous action is not fully handled until the mouse button is released, so the MouseEnter event is not able to raise.
I'm implementing drag & drop from a ListBox, but I'm seeing some strange behaviour with a ContextMenu elsewhere in the window. If you open the context menu and then start a drag from the ListBox, the context menu closes but won't open again until after you perform another drag.
Does this make sense? Anybody got any ideas what might be going on?
<ListBox Grid.Row="0" ItemsSource="{Binding SourceItems}" MultiSelectListboxDragDrop:ListBoxExtension.SelectedItemsSource="{Binding SelectedItems}" SelectionMode="Multiple" PreviewMouseLeftButtonDown="HandleLeftButtonDown" PreviewMouseLeftButtonUp="HandleLeftButtonUp" PreviewMouseMove="HandleMouseMove"/>
<ListBox Grid.Row="1" ItemsSource="{Binding DestinationItems}" AllowDrop="True" Drop="DropOnToDestination" />
<Button Grid.Row="2">
<Button.ContextMenu>
<ContextMenu x:Name="theContextMenu">
<MenuItem Header="context 1"/>
<MenuItem Header="context 2"/>
<MenuItem Header="context 3"/>
</ContextMenu>
</Button.ContextMenu>
Button with context menu
</Button>
...
public partial class Window1
{
private bool clickedOnSourceItem;
public Window1()
{
InitializeComponent();
DataContext = new WindowViewModel();
}
private void DropOnToDestination(object sender, DragEventArgs e)
{
var viewModel = (WindowViewModel)e.Data.GetData(typeof(WindowViewModel));
viewModel.CopySelectedItems();
}
private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var sourceElement = (FrameworkElement)sender;
var hitItem = sourceElement.InputHitTest(e.GetPosition(sourceElement)) as FrameworkElement;
if(hitItem != null)
{
clickedOnSourceItem = true;
}
}
private void HandleLeftButtonUp(object sender, MouseButtonEventArgs e)
{
clickedOnSourceItem = false;
}
private void HandleMouseMove(object sender, MouseEventArgs e)
{
if(clickedOnSourceItem)
{
var sourceItems = (FrameworkElement)sender;
var viewModel = (WindowViewModel)DataContext;
DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move);
clickedOnSourceItem = false;
}
}
}
It seemed to be something to do with the mouse capture!?
The normal sequence of events during a drag goes something like this...
The PreviewMouseLeftButtonDown
handler gets called and
ListBox.IsMouseCaptureWithin is
false.
The PreviewMouseMove handler
gets called. By this time
ListBox.IsMouseCaptureWithin is true.
During the PreviewMouseMove handler
DragDrop.DoDragDrop gets called and
sometime during this the mouse
capture is released from the ListBox.
But, what seems to happening for a drag started when the context menu is open is...
The PreviewMouseLeftButtonDown
handler gets called and
ListBox.IsMouseCaptureWithin is
false.
The PreviewMouseMove handler gets
called. But this time
ListBox.IsMouseCaptureWithin is
still false.
Sometime after the end of the
PreviewMouseMove handler the
ListBox then gets the mouse capture
(ListBox.IsMouseCaptureWithin
becomes true)
The result of this is that after the drag, the ListBox still has the mouse capture so any clicks on the button to open the context menu are actually going to the listbox not the button.
Adding the following code to the start of the PreviewMouseLeftButtonDown handler seems to help by swallowing up the click that closes that context menu rather than trying to start a drag from it...
if (!contextMenuCloseComplete)
{
sourceElement.CaptureMouse();
return;
}
...with the contextMenuCloseComplete bool getting set in handlers for the context menu's Closed and Opened events.
Does that make sense? Does anyone understand where this mouse capture behaviour is coming from?