关于c#:TextBox绑定到已处理的字符串

TextBox binding to a processed string

我的文本框"文本"绑定有问题。这是代码:

1
2
3
4
5
6
<TextBox Padding="2"
    AcceptsReturn="True"
    AcceptsTab="True"
    Name="txtCmd"
    Text="{Binding CommandText,
                   Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>

这是VM属性:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private string _CommandText;
public string CommandText
{
    get
    {
        return this._CommandText;
    }
    set
    {
        if(!(value == this._CommandText))
        {
            this._CommandText = value;
            if (this._CommandText.IndexOf("\
\
"
) > -1)
            {
                ParseCommand(this._CommandText)
                this._CommandText ="";
            }
            base.OnPropertyChanged("CommandText");
        }
    }
}

问题是:当我在文本框中按Enter时,ParseCommand会调用并执行其必须做的任何事情,而且_CommandText将设置为("),但它不会影响textbox.text值,我的意思是,下次按Enter键时,仍然会有一些'\\
\\
在_CommandText中!

我有做错什么吗?任何帮助将不胜感激。


我认为您最好以其他方式进行处理。您确实不需要绑定文本,因为您不想同步数据而是处理输入,因此使用输入事件可能是一个更好的主意。

这可以与命令和Blend SDK的Interactivity(例如

)结合使用

1
2
3
4
5
6
7
8
<TextBox Padding="2">
    <i:Interaction.Triggers>
        <t:KeyDownTrigger Key="Return">
            <i:InvokeCommandAction Command="{Binding ProcessCommandCommand}"
                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=TextBox}}" />
        </t:KeyDownTrigger>
    </i:Interaction.Triggers>
</TextBox>

KeyDownTrigger的定义如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class KeyDownTrigger : TriggerBase<TextBox>
{
    public Key Key { get; set; }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(AssociatedObject_PreviewKeyDown);
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
    }

    void AssociatedObject_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == Key)
        {
            InvokeActions(null);
        }
    }
}

该命令将类似于以下内容:

1
2
3
4
5
6
7
8
private readonly Command _ProcessCommandCommand = new Command(p =>
    {
        var tb = (TextBox)p;
        var command = tb.Text;
        // <Process command>
        tb.Clear();
    });
public Command ProcessCommandCommand { get { return _ProcessCommandCommand; } }

(Command是人们喜欢称呼的RelayCommand,它是ICommand的通用实现,它使用在构造函数中传递的函数)


我不知道TextBox出现此行为的确切原因,但是要解决您的问题,请设置_CommandText = null而不是"。这是示例代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private string _CommandText;
public string CommandText
{
    get
    {
        return this._CommandText;
    }
    set
    {
        if(!(value == this._CommandText))
        {
            this._CommandText = value;
            if (this._CommandText.Contains("\
\
"
))
            {
                ParseCommand(this._CommandText)
                this._CommandText = null;
            }
            base.OnPropertyChanged("CommandText");
        }
    }
}