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

MVVM在viewmodel上创建文本框并设置绑定,mvvm,binding,textbox,viewmodel,dynamically-generated,Mvvm,Binding,Textbox,Viewmodel,Dynamically Generated,我在视图上有一个空的stackpanel,我需要在ViewModel上添加文本框。同时我需要为这个文本框设置绑定到文本属性 视图: <StackPanel> <ItemsControl ItemsSource="{Binding Path=myBtCollection}"></ItemsControl> </StackPanel> 视图模型: public decimal tex

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

视图:

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

视图模型:

        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; 
}
公共十进制文本示例{get;set;}
公共十进制文本Example2{get;set;}
公共十进制文本Example3{get;set;}
公共ObservableCollection myTxCollection{get;set;}
公共示例ViewModel()
{
myTxCollection=新的ObservableCollection();
TextBox txt1=newtextbox{Text=textExample.ToString()};
TextBox txt2=newtextbox{Text=textextextexexample2.ToString()};
TextBox txt3=newtextbox{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。然后我触发了example命令,属性发生了变化,但是视图没有显示这些变化。文本框仍显示为0。必须如何为此设置绑定?
谢谢

您需要在包含类上实现
INotifyPropertyChanged
,这不是MVVM。视图模型不应创建任何UI元素,而应创建UI可以绑定到的数据。我建议您阅读更多关于MVVM的基础知识。