从Viewmodel更改的文本框值未反映在UI上 [英] Textbox value changed from Viewmodel not reflected on UI

查看:169
本文介绍了从Viewmodel更改的文本框值未反映在UI上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,文本绑定到ViewModel中的一个属性。用户可以手动输入文本,也可以从剪贴板粘贴文本。
我解析用户输入的文本(我使用 UpdateSourceTrigger = PropertyChanged ),并用换行符char拆分文本。

I have a textbox, text is bound to a property in ViewModel. User can either manually enter the text or paste it from clipboard. I parse the text (I use UpdateSourceTrigger=PropertyChanged) entered by the user and split the text by newline char.

问题:当用户点击Enter时,一切正常。但是,当我尝试处理粘贴的文本时,只要看到第一个 \n,我就会尝试将其分解为不同的字符串并清除文本框。在ViewModel中,文本设置为string.empty,但不会反映在UI上。

Problem: When user hits enter, everything works fine. But when I try to process the pasted text, as soon as I see first "\n", I try to break it into different strings and clear the textbox. In ViewModel, text is set to string.empty, but it is not reflected on the UI.

代码有什么问题?我知道在自己的setter属性中编辑文本不是一种好的编码习惯,但是我该如何做呢?

What is wrong in the code? I understand that editing the text in its own setter property is not good coding practice, but how can I do it otherwise?

这是代码段:

XAML

    <TextBox AcceptsReturn="True" VerticalAlignment="Stretch" BorderBrush="Transparent"
             Text="{Binding TextBoxData, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
        <TextBox.InputBindings>
            <KeyBinding Key="Enter" Command="{Binding OnNewLineCommand}"/>
        </TextBox.InputBindings>
    </TextBox>

ViewModel:

ViewModel:

     public string TextBoxData
        {
            get
            {
                return _textBoxData;
            }
            set
            {
                _textBoxData = value;
                RaisePropertyChanged("TextBoxData");
                if(_textBoxData != null && _textBoxData.Contains("\n"))
                {
                    OnNewLineCommandEvent(null);
                }
            }
        }

    public DelegateCommand<string> OnNewLineCommand
    {
        get
        {
            if (_onNewLineCommand == null)
            {
                _onNewLineCommand = new DelegateCommand<string>(OnNewLineCommandEvent);
            }
            return _onNewLineCommand;
        }
    }

    private void OnNewLineCommandEvent(string obj)
    {
        if (_textBoxData != null && _textBoxData.Length > 0)
        {
            List<string> tbVals = _textBoxData.Split('\n').ToList();
            foreach (string str in tbVals)
            {
                ListBoxItems.Add(new UnitData(str.Trim()));
            }
            TextBoxData = string.Empty;
        }
    }

谢谢,

RDV

推荐答案

我发现了问题所在。如果我尝试在其设置器中修改文本框值,并且未更改该值,则该值不会反映在UI上。下面可能有助于更好地解释这一点:

I figured out the issue. If I try to modify textbox value within its setter and value is not changed, it is not reflected on the UI. Below might help explain this better:


  1. 最初,当UI启动时,VM中的文本框值设置为 ABC。

  2. 用户从UI将此值更改为 XYZ。

  3. 在VM设置程序中,程序员检查文本框值是否为 XYZ,然后将其更改回 ABC->在我的案例中这失败了。 VM中的文本框的值已更改为 ABC,但UI仍将显示 XYZ。

解决方案:在VM中,改为将值设置回 ABC后将其设置为 ABC(请注意额外的空格)->这将更改UI值。

Solution: In VM, instead of setting value back to "ABC" set it to "ABC " (Notice the additional space) -> this will change the UI value.

更新后的VM代码:

        private bool _isTextValChanged = false;
    private void OnNewLineCommandEvent(string obj)
    {
        if (_textBoxData != null && _textBoxData.Length > 0)
        {
            List<string> tbVals = _textBoxData.Split('\n').ToList();
            foreach (string str in tbVals)
            {
                ListBoxItems.Add(new UnitData(str.Trim()));
            }
            //This will toggle textbox value and updated value will be reflected on the UI.
            if (_isTextValChanged)
                TextBoxData = " ";
            else
                TextBoxData = string.Empty;

            _isTextValChanged = !_isTextValChanged;
        }
    }

谢谢,

RDV

这篇关于从Viewmodel更改的文本框值未反映在UI上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆