Skip to content

Commit

Permalink
Wpf: This adds a UseMouseSelectionOnly property to TextBoxCell/ImageT…
Browse files Browse the repository at this point in the history
…extCell to use from a native style.

Sometimes you may wish the mouse in a TextBoxCell and ImageTextCell to only select text but not drag selected text.  This adds a property to the handlers so you can apply it with a style. E.g.

    Style.Add<Eto.Wpf.Forms.Cells.TextBoxCellHandler>("no-mouse-drag", handler => handler.UseMouseSelectionOnly = true);
    Style.Add<Eto.Wpf.Forms.Cells.ImageTextCellHandler>("no-mouse-drag", handler => handler.UseMouseSelectionOnly = true);
  • Loading branch information
cwensley committed Aug 30, 2017
1 parent 4114bba commit 42066c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
21 changes: 20 additions & 1 deletion Source/Eto.Wpf/Forms/Cells/ImageTextCellHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Eto.Forms;
using swc = System.Windows.Controls;
using sw = System.Windows;
Expand All @@ -12,6 +12,18 @@ namespace Eto.Wpf.Forms.Cells
{
public class ImageTextCellHandler : CellHandler<ImageTextCellHandler.Column, ImageTextCell, ImageTextCell.ICallback>, ImageTextCell.IHandler
{
static readonly object UseMouseSelectionOnly_Key = new object();

/// <summary>
/// This makes the mouse only select text without being able to drag/drop the text.
/// By default, when clicking and dragging a selection it would allow you to drag the text to other controls.
/// </summary>
public bool UseMouseSelectionOnly
{
get { return Widget.Properties.Get(UseMouseSelectionOnly_Key, false); }
set { Widget.Properties.Set(UseMouseSelectionOnly_Key, value); }
}

public TextAlignment TextAlignment
{
get { return Control.TextAlignment.ToEto(); }
Expand Down Expand Up @@ -177,6 +189,13 @@ protected override sw.FrameworkElement GenerateEditingElement(swc.DataGridCell c
if (Handler.AutoSelectMode == AutoSelectMode.OnFocus)
control.SelectAll();
};
if (Handler.UseMouseSelectionOnly)
{
element.PreviewMouseLeftButtonDown += (sender, e) =>
{
element.Select(0, 0);
};
}
element.DataContextChanged += (sender, e) =>
{
var control = sender as swc.TextBox;
Expand Down
21 changes: 20 additions & 1 deletion Source/Eto.Wpf/Forms/Cells/TextBoxCellHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using Eto.Forms;
using swc = System.Windows.Controls;
using swd = System.Windows.Data;
Expand All @@ -10,6 +10,18 @@ namespace Eto.Wpf.Forms.Cells
{
public class TextBoxCellHandler : CellHandler<TextBoxCellHandler.Column, TextBoxCell, TextBoxCell.ICallback>, TextBoxCell.IHandler
{
static readonly object UseMouseSelectionOnly_Key = new object();

/// <summary>
/// This makes the mouse only select text without being able to drag/drop the text.
/// By default, when clicking and dragging a selection it would allow you to drag the text to other controls.
/// </summary>
public bool UseMouseSelectionOnly
{
get { return Widget.Properties.Get(UseMouseSelectionOnly_Key, false); }
set { Widget.Properties.Set(UseMouseSelectionOnly_Key, value); }
}

public TextAlignment TextAlignment
{
get { return Control.TextAlignment.ToEto(); }
Expand Down Expand Up @@ -104,6 +116,13 @@ protected override sw.FrameworkElement GenerateEditingElement(swc.DataGridCell c
element.SetBinding(swc.TextBlock.VerticalAlignmentProperty, CreateBinding(nameof(VerticalAlignment)));
element.Text = Handler.GetValue(dataItem);
Handler.FormatCell(element, cell, dataItem);
if (Handler.UseMouseSelectionOnly)
{
element.PreviewMouseLeftButtonDown += (sender, e) =>
{
element.Select(0, 0);
};
}

element.DataContextChanged += (sender, e) =>
{
Expand Down

0 comments on commit 42066c9

Please sign in to comment.