如何在 Mahapps Metro 输入框中指定插入符位置?
How to specify caret position in Mahapps Metro inputbox?
我正在使用 ShowInputAsync 显示输入框。一些预定义文本使用 DefaultText 设置。
MetroDialogSettings settings = new MetroDialogSettings() { DefaultText = "Some text" };
var result = await window.ShowInputAsync("Some title", "Some message", settings);
如何在 DefaultText 之后放置插入符号?
用户应该能够附加到这个默认文本并在它不符合人体工程学之前使用插入符号...
here.
描述了在 TextBox 末尾设置插入符号的好方法
您只需要找到 InputDialog 的 TextBox。实现 InputDialog 的 UserControl 是 MahApps.Metro.Controls.Dialogs.InputDialog
,所需的文本框名为 PART_TextBox
。有多种可能的解决方案,我建议您创建混合行为并使用样式附加它(概念取自 here)。
行为代码:
public class BaseMetroDialogAdjustTextBehavior : Behavior<InputDialog>
{
public static DependencyProperty IsAttachedProperty =
DependencyProperty.RegisterAttached("IsAttached",
typeof(bool),
typeof(BaseMetroDialogAdjustTextBehavior),
new FrameworkPropertyMetadata(false, OnIsAttachedChanged));
private TextBox inputTextBox;
public static bool GetIsAttached(DependencyObject uie)
{
return (bool)uie.GetValue(IsAttachedProperty);
}
public static void SetIsAttached(DependencyObject uie, bool value)
{
uie.SetValue(IsAttachedProperty, value);
}
private static void OnIsAttachedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UIElement element = obj as UIElement;
if (element == null)
return;
{
var behaviors = Interaction.GetBehaviors(element);
var existingBehavior = behaviors.OfType<BaseMetroDialogAdjustTextBehavior>().FirstOrDefault();
if ((bool)e.NewValue == false && existingBehavior != null)
{
behaviors.Remove(existingBehavior);
}
else if ((bool)e.NewValue == true && existingBehavior == null)
{
behaviors.Add(new BaseMetroDialogAdjustTextBehavior());
}
}
}
protected override void OnAttached()
{
inputTextBox = AssociatedObject.FindName("PART_TextBox") as TextBox;
inputTextBox.GotFocus += inputTextBox_GotFocus;
}
protected override void OnDetaching()
{
inputTextBox.GotFocus -= inputTextBox_GotFocus;
}
void inputTextBox_GotFocus(object sender, RoutedEventArgs e)
{
inputTextBox.CaretIndex = inputTextBox.Text.Length;
}
}
为了附加此行为,您可以将以下代码放入 app.xaml
<Application
...
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:local="clr-namespace:THE_NAMESPASE_OF_BEHAVIOR">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Dialogs:InputDialog}">
<Style.Setters>
<Setter Property="local:BaseMetroDialogAdjustTextBehavior.IsAttached" Value="True"/>
</Style.Setters>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>
我正在使用 ShowInputAsync 显示输入框。一些预定义文本使用 DefaultText 设置。
MetroDialogSettings settings = new MetroDialogSettings() { DefaultText = "Some text" };
var result = await window.ShowInputAsync("Some title", "Some message", settings);
如何在 DefaultText 之后放置插入符号?
用户应该能够附加到这个默认文本并在它不符合人体工程学之前使用插入符号...
here.
描述了在 TextBox 末尾设置插入符号的好方法
您只需要找到 InputDialog 的 TextBox。实现 InputDialog 的 UserControl 是 MahApps.Metro.Controls.Dialogs.InputDialog
,所需的文本框名为 PART_TextBox
。有多种可能的解决方案,我建议您创建混合行为并使用样式附加它(概念取自 here)。
行为代码:
public class BaseMetroDialogAdjustTextBehavior : Behavior<InputDialog>
{
public static DependencyProperty IsAttachedProperty =
DependencyProperty.RegisterAttached("IsAttached",
typeof(bool),
typeof(BaseMetroDialogAdjustTextBehavior),
new FrameworkPropertyMetadata(false, OnIsAttachedChanged));
private TextBox inputTextBox;
public static bool GetIsAttached(DependencyObject uie)
{
return (bool)uie.GetValue(IsAttachedProperty);
}
public static void SetIsAttached(DependencyObject uie, bool value)
{
uie.SetValue(IsAttachedProperty, value);
}
private static void OnIsAttachedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
UIElement element = obj as UIElement;
if (element == null)
return;
{
var behaviors = Interaction.GetBehaviors(element);
var existingBehavior = behaviors.OfType<BaseMetroDialogAdjustTextBehavior>().FirstOrDefault();
if ((bool)e.NewValue == false && existingBehavior != null)
{
behaviors.Remove(existingBehavior);
}
else if ((bool)e.NewValue == true && existingBehavior == null)
{
behaviors.Add(new BaseMetroDialogAdjustTextBehavior());
}
}
}
protected override void OnAttached()
{
inputTextBox = AssociatedObject.FindName("PART_TextBox") as TextBox;
inputTextBox.GotFocus += inputTextBox_GotFocus;
}
protected override void OnDetaching()
{
inputTextBox.GotFocus -= inputTextBox_GotFocus;
}
void inputTextBox_GotFocus(object sender, RoutedEventArgs e)
{
inputTextBox.CaretIndex = inputTextBox.Text.Length;
}
}
为了附加此行为,您可以将以下代码放入 app.xaml
<Application
...
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
xmlns:local="clr-namespace:THE_NAMESPASE_OF_BEHAVIOR">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type Dialogs:InputDialog}">
<Style.Setters>
<Setter Property="local:BaseMetroDialogAdjustTextBehavior.IsAttached" Value="True"/>
</Style.Setters>
</Style>
</ResourceDictionary>
</Application.Resources>
</Application>