C# 如何在Mahapps Metro输入框中指定插入符号位置?
我正在使用ShowInputSync显示输入框。某些预定义文本使用DefaultText设置C# 如何在Mahapps Metro输入框中指定插入符号位置?,c#,wpf,mahapps.metro,C#,Wpf,Mahapps.metro,我正在使用ShowInputSync显示输入框。某些预定义文本使用DefaultText设置 MetroDialogSettings settings = new MetroDialogSettings() { DefaultText = "Some text" }; var result = await window.ShowInputAsync("Some title", "Some message", settings); 如何在DefaultText之后定位插入符号 用户应该能够附加到
MetroDialogSettings settings = new MetroDialogSettings() { DefaultText = "Some text" };
var result = await window.ShowInputAsync("Some title", "Some message", settings);
如何在DefaultText之后定位插入符号
用户应该能够附加到此默认文本并在其不符合人体工程学之前使用插入符号…在所述文本框的末尾设置插入符号的好方法。
您只需找到InputDialog的文本框。实现InputDialog的用户控件是
MahApps.Metro.Controls.Dialogs.InputDialog
和名为PART\u TextBox
的所需文本框。有多种可行的解决方案,我建议您创建混合行为,并使用样式(概念取自)附加它
行为准则:
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;
}
}
公共类BaseMetroDialogAdjustTextBehavior:Behavior
{
公共静态从属属性IsAttachedProperty=
DependencyProperty.RegisterAttached(“IsAttached”,
类型(bool),
typeof(BaseMetroDialogAdjustTextBehavior),
新的FrameworkPropertyMetadata(false,OnIsAttachedChanged));
私有文本框输入文本框;
公共静态布尔GetIsAttached(DependencyObject uie)
{
返回(bool)uie.GetValue(IsAttachedProperty);
}
公共静态无效集合已附加(DependencyObject uie,布尔值)
{
uie.SetValue(IsAttachedProperty,值);
}
已更改SatAttachedChanged上的私有静态无效(DependencyObject对象、DependencyPropertyChangedEventArgs e)
{
UIElement=obj作为UIElement;
if(元素==null)
返回;
{
var行为=Interaction.GetBehaviors(元素);
var existingBehavior=behaviors.OfType().FirstOrDefault();
if((bool)e.NewValue==false&&existingBehavior!=null)
{
删除(现有行为);
}
else如果((bool)e.NewValue==true&&existingBehavior==null)
{
添加(新的BaseMetroDialogAdjustTextBehavior());
}
}
}
受保护的覆盖无效附加()
{
inputTextBox=AssociatedObject.FindName(“PART_TextBox”)作为TextBox;
InputExtBox.GotFocus+=InputExtBox\u GotFocus;
}
附加时受保护的覆盖无效()
{
InputExtBox.GotFocus-=InputExtBox\u GotFocus;
}
无效输入ExtBox_GotFocus(对象发送器,路由目标e)
{
InputExtBox.CaretIndex=InputExtBox.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>
这就是为什么WPF需要在烤箱中再次进行良好烘焙的原因。我同意这并不简单。在WPF中,只有最基本的东西是简单的,一切都是复杂的。。。当我意识到这一点的时候,我正在开发一个规模可观的项目,而不可能轻易返回。现在,代码中充满了这样的答案,我们将在几年后看到它,并想知道为什么我们会浪费时间:-)顺便说一句,如果你从源代码编译Mahapps,你可以使用一行代码实现相同的功能PART_TextBox.GotFocus+=(s,e)=>PART_TextBox.CaretIndex=PART_TextBox.Text.Length
OnDetaching()方法应该取消订阅,而不是再次订阅:``inputTextBox.GotFocus-=inputTextBox\u GotFocus;