MVVM在viewmodel上创建文本框并设置绑定

时间:2014-02-19 08:24:03

标签: mvvm binding textbox viewmodel dynamically-generated

我在View上有一个空的stackpanel,我需要在ViewModel上添加文本框。同时我需要为这个文本框设置绑定到Text属性。

查看:

    <StackPanel>
                    <ItemsControl ItemsSource="{Binding Path=myBtCollection}"></ItemsControl>
</StackPanel>

VIEWMODEL:

        public decimal textExample { get; set; }
        public decimal textExample2 { get; set; }
        public decimal textExample3 { get; set; }


        public ObservableCollection<TextBox> myTxCollection { get; set; }

public ExampleViewModel()
{
            myTxCollection = new ObservableCollection<TextBox>();

            TextBox txt1 = new TextBox { Text = textExample.ToString() }; 
            TextBox txt2 = new TextBox { Text = textExample2.ToString() };
            TextBox txt3 = new TextBox { Text = textExample3.ToString() };

           myTxCollection.Add(txt1);
           myTxCollection.Add(txt2);
           myTxCollection.Add(txt3);
}

public void ExampleCommand()
{
textExample = 123;
textExample2 = 456;
textExample3 = 789; 
}

通过此代码,我可以创建文本框。但是ViewModel中的一些方法(ExampleCommands),我改变了textExample属性的值。 摘要代码第一次运行;文本框已成功创建,文本值为0.然后我触发了ExampleCommand,属性已更改但View未显示此更改。文本框仍显示为0.必须如何为此设置绑定? 感谢

0 个答案:

没有答案