我正在使用ShowInputAsync来显示输入框。使用DefaultText设置一些预定义文本。
MetroDialogSettings settings = new MetroDialogSettings() { DefaultText = "Some text" };
var result = await window.ShowInputAsync("Some title", "Some message", settings);
如何在DefaultText之后放置插入符号?
用户应该能够在不符合人体工程学的情况下附加到此默认文本并使用插入符号...
答案 0 :(得分:1)
在TextBox结尾处设置carret的好方法here
您只需找到InputDialog的TextBox即可。实现InputDialog的UserControl为MahApps.Metro.Controls.Dialogs.InputDialog
,所需的texbox名为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>