Keep a Silverlight Combobox DropDown open after losing focus - silverlight

Is it possible to avoid the automatic collapse of a Silverlight ComboBox after LostFocus?

Well, looking at the disassembled code it looks like
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
this.FocusChanged(this.HasFocus());
}
is a good candidate for overwritting.
There's no way to solve your problem without implementing your own control subclass.
I've done the same to have a ComboBox with a Popup that doesn't close when I select an item (I want to have a multi-select behaviour).
If anyone is interested, here are my classes (works just fine for me as it is):
public class ComboBoxWithMultiSelect : ComboBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (base.IsDropDownOpen &&
(e.Key == Key.Enter ||
e.Key == Key.Space))
{
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
protected override DependencyObject GetContainerForItemOverride()
{
return new ComboBoxItemWithMultiSelect();
}
}
public class ComboBoxItemWithMultiSelect : ComboBoxItem
{
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
if (e == null)
{
throw new ArgumentNullException("e");
}
if (!e.Handled)
{
e.Handled = true;
}
}
}

I don't think there is an easy way around this. The code below is copied from the disassembled code from the ComboBox Class. As you can see it closes always when hasFocus is false. I don't think there is any way around this. Writing your own ComboBox is a solution.
private void FocusChanged(bool hasFocus)
{
this.UpdateSelectionBoxHighlighted();
base.SetValueInternal(IsSelectionActiveProperty, hasFocus, true);
if (!hasFocus)
{
this.IsDropDownOpen = false;
}
}

Related

How to Cancel Datagrid selection changed event in WPF?

I know this question is asked before, but I couldnt find what I am looking for.
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (oOrdItem.ItemNo == 0)
{
e.Handled = true;
MessageBox.Show("Please save the order item", "Save");
return;
}
}
Even if I call e.Handled = true;
it will select the datagrid row. I dont want to call dataGrid1.SelectedIndex =-1; because it will trigger selectionchanged event again. I also tried dataGrid1.UnSelectAll();
Any other way to cancel the selectionchanged event?
I used a variety of methods to try to cancel the selection changed event, including the method from the selected answer, but none of them worked. This, however, worked great for me:
Using the PreviewMouseDown event-handler for the datagrid:
private void dataGrid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
//get the item I am clicking on (replace MyDataClass with datatype in datagrid)
var myItem = (e.OriginalSource as FrameworkElement).DataContext as MyDataClass;
//check if item is different from currently selected item
if (myItem != dataGrid.SelectedItem)
{
//save message dialog
MessageBoxResult result = MessageBox.Show("Changes will be lost. Are you sure?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question);
//if click no, then cancel the event
if (result == MessageBoxResult.No)
{
e.Handled = true;
}
else
{
//otherwise, reinvoke the click event
dataGrid.Dispatcher.BeginInvoke(
new Action(() =>
{
RoutedEventArgs args = new MouseButtonEventArgs(e.MouseDevice, 0, e.ChangedButton);
args.RoutedEvent = UIElement.MouseDownEvent;
(e.OriginalSource as UIElement).RaiseEvent(args);
}),
System.Windows.Threading.DispatcherPriority.Input);
}
}
}
}
This successfully keeps the current row selected if the user clicks "No", and if they click "Yes", then execution will continue as normal. Hopefully this helps someone in the future, because it took a long time to find something that would work for a seemingly simple problem.
Did you think about an alternative implementation? I'm thinking on Binding and a check method before changing the SelectedItem. An illustration:
<DataGrid ItemsSource="..." SelectedItem="{Binding SelectedEntry}" />
and the underlying VM could look like this:
public class SampleVm : ViewModelBase//assuming that you are using such a base class
{
private object _selectedEntry;
public object SelectedEntry
{
get { return _selectedEntry; }
set
{
if (!SavePrevItem())
return;
_selectedEntry = value;
RaisePropertyChanged("SelectedItem"); // or something similar
}
}
private bool SavePrevItem()
{
// your logic here
}
}

WPF closing child- closes parent-window

i have the pretty same sample as mentioned here.
Fast concluded: MainWindow closes when the last childwindow is closed.
My Problem: I couldn't solve my problems with the described solutions. I can't produce a program where it als takes place. Only in one of my bigger progs. Maybe someone has an idea or knows any further steps.
Thanks for reading - Thomas
As requested here's a bit of code:
This is the part in the MainWindow:
bool editAfterSearch = false;
Movie selectedMovie = (Movie)this.listView.SelectedItem;
Movie backup = (Movie)selectedMovie.Clone();
if (new OnlineSearchWindow().EditMovieViaOnlineSearch(ref selectedMovie, out editAfterSearch))
{
this.coverFlow.Update(selectedMovie);
}
And that's the part of the ChildWindow:
public bool EditMovieViaOnlineSearch(ref Movie preset, out bool editAfter)
{
this.exitWithOk = false;
this.editMovieAfterSearch = false;
this.tbx_SearchTerm.Text = preset.Title;
this.linkedMovie = preset;
this.ShowDialog();
editAfter = editMovieAfterSearch;
if (this.exitWithOk)
{
this.linkedMovie.CloneOnlineInformation(ref preset);
preset.Bitmap = this.linkedMovie.Bitmap;
return true;
}
else
{
return false;
}
}
Try playing with the ShutDownMode property of your App.xaml.cs. The 3 values are OnMainWindowClose, OnLastWindowClose, and OnExplicitShutdown, and the default is OnLastWindowClose
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
this.ShutdownMode = System.Windows.ShutdownMode.OnMainWindowClose;
}
}
The below code worked for me.
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (MessageBoxResult.No == (MessageBox.Show("Are you sure you want to close this?.", "ProjectName", MessageBoxButton.YesNo)))
{
e.Cancel = true;
foreach (var item in Application.Current.Windows)
{
Window window = item as Window;
if (window.Title == "PopUpWindowName")
{
window.Topmost = true;
break;
}
}
return;
}
else
{
base.OnClosed(e);
Application.Current.Shutdown();
}
}
you can try setting the child window's allowShutDown to false and then show the mainwindow. I'm assuming you will start with mainwindow's visibility set to hidden.
Application.Current.MainWindow.Visibility = System.Windows.Visibility.Visible;
this.allowShutDown = false;
The allowShutDown will be your own property which u can set to enable you have to handle the closing event.

Drag-select with ListBox

I have a simple implementation of a ListView in WPF that allows me to select multiple items in the list by holding the mouse button and dragging over the items. However, while holding the mouse button down, when I move the mouse outside the ListView, something strange happens with the selection. Ideally, I would just want the selection to remain the same, but instead it quickly cycles through all the selected items, leaving only the last item selected.
Here's the code, have any ideas?
public class MultiSelectListView : ListView
{
private bool m_isSelectionActive;
public bool IsSelectionActive
{
get { return m_isSelectionActive; }
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MultiSelectListViewItem(this);
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
m_isSelectionActive = true;
}
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
{
m_isSelectionActive = false;
}
}
public class MultiSelectListViewItem : ListViewItem
{
private readonly MultiSelectListView m_parent;
public MultiSelectListViewItem(MultiSelectListView parent)
{
m_parent = parent;
}
protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
{
if (m_parent.IsSelectionActive)
IsSelected = true;
}
}
The funkiness you are experiencing happens when the mouse "drag" goes above the top of the list or below the bottom of the list. I think the behavior you set up will only work well if the selection mode is Multiple. The modifications to the MultiSelectListView below set the default selection mode to Multiple and assumes the user wants to start another selection with a left mouse click. You will still experience funkiness if the SelectionMode is set to Extended or Single in the XAML.
public class MultiSelectListView : ListView
{
private bool m_isSelectionActive;
public bool IsSelectionActive
{
get
{
return m_isSelectionActive;
}
}
protected override DependencyObject GetContainerForItemOverride()
{
return new MultiSelectListViewItem(this);
}
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (SelectionMode != SelectionMode.Single)
{
SelectedItems.Clear();
}
m_isSelectionActive = true;
}
protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
{
m_isSelectionActive = false;
}
public MultiSelectListView() : base()
{
SelectionMode = SelectionMode.Multiple;
}
}

Disable right click "Silverlight" popup in comboboxes

Hi
I'm trying to get rid of the annoying "About Silverlight" context menu that pops up whenever you right click in a Silverlight application. I've added the usual ways:
In App.xaml
rootVisual.MouseRightButtonDown += ((s, args) => args.Handled = true);
and the same for all ChildWindows.
The problem that persist is in all "pop up"-controls like comboboxes and datepicker calender popup. There I can't get rid of it. I would like to handle the right click in a style that I can make implicit for the entire application. Is this possible? Can I solve it some other smart way?
Best
Daniel
The answer was to inherit the combobox and make a custom control like this:
public class CellaComboBox : ComboBox
{
public CellaComboBox()
{
DropDownOpened += _dropDownOpened;
DropDownClosed += _dropDownClosed;
}
private static void _dropDownClosed(object sender, EventArgs e)
{
HandlePopupRightClick(sender, false);
}
private static void _dropDownOpened(object sender, EventArgs e)
{
HandlePopupRightClick(sender, true);
}
private static void HandlePopupRightClick(object sender, bool hook)
{
ComboBox box = (ComboBox)sender;
var popup = box.GetChildElement<Popup>();
if (popup != null)
{
HookPopupEvent(hook, popup);
}
}
static void HookPopupEvent(bool hook, Popup popup)
{
if (hook)
{
popup.MouseRightButtonDown += popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown += popup_MouseRightButtonDown;
}
else
{
popup.MouseRightButtonDown -= popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown -= popup_MouseRightButtonDown;
}
}
static void popup_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}
with the extension method for framworkelement looking like this:
public static class FrameworkElementExtensions
{
public static TType GetChildElement<TType>(this DependencyObject parent) where TType : DependencyObject
{
TType result = default(TType);
if (parent != null)
{
result = parent as TType;
if (result == null)
{
for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); ++childIndex)
{
var child = VisualTreeHelper.GetChild(parent, childIndex) as FrameworkElement;
result = GetChildElement<TType>(child) as TType;
if (result != null) return result;
}
}
}
return result;
}
}
You need to handle the DatePicker in the same way but instead of DropDownOpened and DropDownClosed you use CalenderOpened and CalenderClosed
C# Corner has an article for fixing the about popup on Silverlight 3:
Disable Context Menu in Silverlight 3 Application

How do I give the focus to a TextBox in a DataForm?

I've got a small DataForm and I want to set the focus on the first TextBox. I'm using the Novermber 2009 Toolkit. I've named the TextBox and tried using .Focus() from the DataForm's loaded event. I see it get focus for one cursor 'blink' and then it's gone. I'm trying to work out if this is an artefact of the DataForm or something else. Does anyone know if I should be able to do this?
A little trick I've used successfully is to subscribe to the Loaded event of the textbox, then in the event handler, I set the focus with code such as this:
private void TextBox_Loaded(object sender, RoutedEventArgs e)
{
TextBox usernameBox = (TextBox)sender;
Dispatcher.BeginInvoke(() => { usernameBox.Focus(); });
}
I tried loads of suggestions e.g. using Dispatcher, UpdateLayout etc etc. floating around on various internet sites and none of them worked reliably for me. In the end I settled on the following:
private bool _firstTime = true;
private void MyChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
if (_firstTime)
{
try
{
var dataForm = MyDataForm;
var defaultFocus = dataForm.FindNameInContent("Description") as TextBox;
defaultFocus.Focus();
}
catch (Exception)
{
}
finally
{
_firstTime = false;
}
}
}
Not pretty I know...but it works. There appears to be a timing issue with using the Focus() method in SL4.
Try calling my custom focus setting function (FocusEx).
internal static class ControlExt
{
// Extension for Control
internal static bool FocusEx(this Control control)
{
if (control == null)
return false;
bool success = false;
if (control == FocusManager.GetFocusedElement())
success = true;
else
{
// To get Focus() to work properly, call UpdateLayout() immediately before
control.UpdateLayout();
success = control.Focus();
}
ListBox listBox = control as ListBox;
if (listBox != null)
{
if (listBox.SelectedIndex < 0 && listBox.Items.Count > 0)
listBox.SelectedIndex = 0;
}
return success;
}
}
That should work for you.
Jim McCurdy
YinYangMoney

Resources