i would like to set 'ShowFocusCues' to false
http://msdn.microsoft.com/en-US/library/system.windows.forms.control.showfocuscues(v=vs.80).aspx
protected public:
virtual property bool ShowFocusCues {
bool get ();
}
How to set/change/remove focus style on a Button in C#?
i tried:
virtual property bool ShowFocusCues {
bool get ();
}
bool ShowFocusCues() override
{
get
{
return false;
}
}
but both is wrong and complains about 'get'
i want to prevent the focus
Have you tried something like this.
protected override bool ShowFocusCues { get { return false; } }
This must be included in code of a control class that must hied focus rectangle from being drawn
Related
I am developing an avalonia wpf application, and I registered an AttachedProperty "IsFocused" as following:
public class FocusExtension
{
public static readonly AttachedProperty<bool> IsFocusedProperty =
AvaloniaProperty.RegisterAttached<Control, bool>("IsFocused", typeof(FocusExtension));
public static bool GetIsFocused(Control element)
{
return element.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(Control element, bool value)
{
element.SetValue(IsFocusedProperty, value);
OnIsFocusedPropertyChanged(element, value);
}
private static void OnIsFocusedPropertyChanged(
Control element,
bool e)
{
if (e)
{
element.Focus();
}
}
}
And apply it in xaml like that:
<Button Content="Test" u:FocusExtension.IsFocused="{Binding SomeBoolPropertyInViewModel}"/>
But it seems not work when "SomeBoolPropertyInViewModel" is set to be true from my ViewModel,
Could someone give me an example or hint to implement this work? Thanks.
There's no guarantee that SetIsFocused will be called, as element.SetValue(IsFocusedProperty, value) may be called directly. So you have to add a handler for the property changed event, in the static constructor:
static FocusExtension()
{
IsFocusedProperty.Changed.AddClassHandler<Control>(OnIsFocusedPropertyChanged);
}
private static void OnIsFocusedPropertyChanged(
Control element,
AvaloniaPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
element.Focus();
}
}
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;
}
}
When inheriting a control in Silverlight, how do I find out if its template has already been applied?
I.e., can I reliably get rid of my cumbersome _hasTemplateBeenApplied field?
public class AwesomeControl : Control
{
private bool _hasTemplateBeenApplied = false;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
this._hasTemplateBeenApplied = true;
// Stuff
}
private bool DoStuff()
{
if (this._hasTemplateBeenApplied)
{
// Do Stuff
}
}
}
Nope that is the standard way to track whether the template has been applied.
I have a Silverlight DataGrid of which I need to check if it has Focus. I know there is a method to set Focus and an event for GotFocus but can't see anyhting for checking if it has focus.
Any Ideas ?
AFAIK there is no direct method or property to check if it has focus, but you should be able to use the FocusManager.GetFocusedElement().
If you then define a extension method, you should be able to call MyDataGrid.HasFocus():
public static class ControlExtensions
{
public static bool HasFocus(this Control aControl)
{
return System.Windows.Input.FocusManager.GetFocusedElement() == aControl;
}
}
[edited: I did test it now:]
However there is catch: the call GetFocusedElement() can return the current focused cell within the DataGrid. So in that case the HasFocus will return false.
To be able to check if the DataGrid or one of its cells are focused, we can adapt our extension method like this
public static class ControlExtensions
{
public static bool HasFocus(this Control aControl, bool aCheckChildren)
{
var oFocused = System.Windows.Input.FocusManager.GetFocusedElement() as DependencyObject;
if (!aCheckChildren)
return oFocused == aControl;
while (oFocused != null)
{
if (oFocused == aControl)
return true;
oFocused = System.Windows.Media.VisualTreeHelper.GetParent(oFocused);
}
return false;
}
}
Hope this helps a bit?
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;
}
}