【问题标题】:How to get CaretIndex value of TextBox control in the view model?如何在视图模型中获取 TextBox 控件的 CaretIndex 值?
【发布时间】:2017-05-17 20:14:30
【问题描述】:

由于CaretIndexTextBox 控件不是DependencyProperty,如何在ViewModel 中获取CaretIndex 的值?

【问题讨论】:

  • 不,不是重复的
  • this question的完全重复
  • 您没有在视图模型中获得插入符号索引。视图关注点不属于视图模型。如果您将它们塞入其中,那么这不是 mvvm,您可以随心所欲地进行操作,包括添加代码隐藏以获取插入符号索引、将数据上下文强制转换为您的视图模型以及更改属性值。跨度>

标签: c# wpf mvvm


【解决方案1】:

你可以Interaction.Behaviors创建你自己的行为并从中获得你想要的属性。

例如。这是EventCommandConverterBehavior。使用它将返回您从xaml 指定的属性值,如果未设置属性名称。它将返回对应的EventArgs

public class EventCommandConverterBehavior : Behavior<FrameworkElement>
{
    private Delegate eventHandler;
    private EventInfo oldEvent;

    // Event
    public string EventName { get { return (string)GetValue(EventNameProperty); } set { SetValue(EventNameProperty, value); } }
    public static readonly DependencyProperty EventNameProperty = DependencyProperty.Register("EventName", typeof(string), typeof(EventCommandConverterBehavior), new PropertyMetadata(null, OnEventChanged));

    // Command
    public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } }
    public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommandConverterBehavior), new PropertyMetadata(null));

    public string PropertyName { get { return (string)GetValue(PropertyNameProperty); } set { SetValue(PropertyNameProperty, value); } }
    public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(string), typeof(EventCommandConverterBehavior), new PropertyMetadata());

    private static void OnEventChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
    {
        var behavior = (EventCommandConverterBehavior)dependencyObject;

        if (behavior.AssociatedObject != null)
            behavior.AttachHandler((string)args.NewValue);
    }

    protected override void OnAttached()
    {
        AttachHandler(this.EventName);
    }

    private void AttachHandler(string eventName)
    {
        // detach old event
        if (this.oldEvent != null)
            this.oldEvent.RemoveEventHandler(this.AssociatedObject, this.eventHandler);

        // attach new event
        if (string.IsNullOrEmpty(eventName))
        {
            return;
        }
        var eventInfo = this.AssociatedObject.GetType().GetEvent(eventName);
        if (eventInfo != null)
        {
            var methodInfo = this.GetType()
                .GetMethod("CommandExecuter", BindingFlags.Instance | BindingFlags.NonPublic);
            this.eventHandler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
            eventInfo.AddEventHandler(this.AssociatedObject, this.eventHandler);
            this.oldEvent = eventInfo;
        }
        else
            throw new ArgumentException(string.Format("Type {0} does not contain event {1} definition.",
                this.AssociatedObject.GetType().Name, eventName));
    }

    private void CommandExecuter(object sender, EventArgs e)
    {
        object parameter = null != PropertyName ? GetPropValue(sender, PropertyName) : e;

        if (this.Command != null)
        {
            if (this.Command.CanExecute(parameter))
                this.Command.Execute(parameter);
        }
    }

    public static object GetPropValue(object src, string propName)
    {
        var propertyInfo = src.GetType().GetProperty(propName);
        return propertyInfo != null ? propertyInfo.GetValue(src, null) : null;
    }
}

Xaml你必须写

<TextBox Name="TextBox1" Text="SomeText" Height="20" Width="60">
    <i:Interaction.Behaviors >
        <wpfPractice:EventCommandConverterBehavior Command="{Binding PropertyValueCommand}" EventName="SelectionChanged" PropertyName="CaretIndex" />
    </i:Interaction.Behaviors>
</TextBox>

PropertyValueCommand 应该是 DataContext 中的命令(即ViewModel

让我知道它是否有效。

【讨论】:

    猜你喜欢
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多