csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
3.7 KiB
104 lines
3.7 KiB
// -----------------------------------------------------------------------
|
|
// <copyright file="TextBox.cs" company="Steven Kirk">
|
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
|
// </copyright>
|
|
// -----------------------------------------------------------------------
|
|
|
|
namespace Perspex.Controls
|
|
{
|
|
using System;
|
|
using System.Reactive.Linq;
|
|
using Perspex.Controls.Primitives;
|
|
|
|
public class TextBox : TemplatedControl
|
|
{
|
|
public static readonly PerspexProperty<bool> AcceptsReturnProperty =
|
|
PerspexProperty.Register<TextBox, bool>("AcceptsReturn");
|
|
|
|
public static readonly PerspexProperty<bool> AcceptsTabProperty =
|
|
PerspexProperty.Register<TextBox, bool>("AcceptsTab");
|
|
|
|
public static readonly PerspexProperty<int> CaretIndexProperty =
|
|
PerspexProperty.Register<TextBox, int>("CaretIndex", coerce: CoerceCaretIndex);
|
|
|
|
public static readonly PerspexProperty<int> SelectionStartProperty =
|
|
PerspexProperty.Register<TextBox, int>("SelectionStart", coerce: CoerceCaretIndex);
|
|
|
|
public static readonly PerspexProperty<int> SelectionEndProperty =
|
|
PerspexProperty.Register<TextBox, int>("SelectionEnd", coerce: CoerceCaretIndex);
|
|
|
|
public static readonly PerspexProperty<string> TextProperty =
|
|
TextBlock.TextProperty.AddOwner<TextBox>();
|
|
|
|
public static readonly PerspexProperty<TextWrapping> TextWrappingProperty =
|
|
TextBlock.TextWrappingProperty.AddOwner<TextBox>();
|
|
|
|
public TextBox()
|
|
{
|
|
var canScrollHorizontally = this.GetObservable(AcceptsReturnProperty)
|
|
.Select(x => !x);
|
|
|
|
this.Bind(
|
|
ScrollViewer.CanScrollHorizontallyProperty,
|
|
canScrollHorizontally,
|
|
BindingPriority.Style);
|
|
|
|
var horizontalScrollBarVisibility = this.GetObservable(AcceptsReturnProperty)
|
|
.Select(x => x ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden);
|
|
|
|
this.Bind(
|
|
ScrollViewer.HorizontalScrollBarVisibilityProperty,
|
|
horizontalScrollBarVisibility,
|
|
BindingPriority.Style);
|
|
}
|
|
|
|
public bool AcceptsReturn
|
|
{
|
|
get { return this.GetValue(AcceptsReturnProperty); }
|
|
set { this.SetValue(AcceptsReturnProperty, value); }
|
|
}
|
|
|
|
public bool AcceptsTab
|
|
{
|
|
get { return this.GetValue(AcceptsTabProperty); }
|
|
set { this.SetValue(AcceptsTabProperty, value); }
|
|
}
|
|
|
|
public int CaretIndex
|
|
{
|
|
get { return this.GetValue(CaretIndexProperty); }
|
|
set { this.SetValue(CaretIndexProperty, value); }
|
|
}
|
|
|
|
public int SelectionStart
|
|
{
|
|
get { return this.GetValue(SelectionStartProperty); }
|
|
set { this.SetValue(SelectionStartProperty, value); }
|
|
}
|
|
|
|
public int SelectionEnd
|
|
{
|
|
get { return this.GetValue(SelectionEndProperty); }
|
|
set { this.SetValue(SelectionEndProperty, value); }
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get { return this.GetValue(TextProperty); }
|
|
set { this.SetValue(TextProperty, value); }
|
|
}
|
|
|
|
public TextWrapping TextWrapping
|
|
{
|
|
get { return this.GetValue(TextWrappingProperty); }
|
|
set { this.SetValue(TextWrappingProperty, value); }
|
|
}
|
|
|
|
private static int CoerceCaretIndex(PerspexObject o, int value)
|
|
{
|
|
var text = o.GetValue(TextProperty);
|
|
var length = (text != null) ? text.Length : 0;
|
|
return Math.Max(0, Math.Min(length, value));
|
|
}
|
|
}
|
|
}
|
|
|