【发布时间】:2015-01-16 19:25:35
【问题描述】:
我正在使用 MVVMM Light WPF,我想要执行以下操作:动态生成文本框并将它们绑定到类的属性。
我已经有以下内容,但在运行应用程序时它没有显示在我的视图中。
这是我的收藏:
private ObservableCollection<Border> _controllekes;
public ObservableCollection<Border> Controllekes
{
get { return _controllekes; }
set
{
_controllekes = value;
RaisePropertyChanged("Controllekes");
}
}
这是我的 xaml:
<ItemsControl ItemsSource="{Binding Path=Controllekes}">
</ItemsControl>
这是我填写itemsource“Controllekes”的部分:
Controllekes = new ObservableCollection<Border>();
Border border = new Border();
border.BorderThickness = new System.Windows.Thickness(5);
border.BorderBrush = Brushes.AliceBlue;
border.Padding = new System.Windows.Thickness(5);
TextBlock tb = new TextBlock();
tb.Background = Brushes.Red;
Binding nameTextBinding = new Binding("Controllekes");
nameTextBinding.Path = new System.Windows.PropertyPath(this.Dossier.Omschrijving);
nameTextBinding.Mode = BindingMode.OneWay;
//nameTextBinding.Source = this.Dossier.Omschrijving;
tb.SetBinding(TextBlock.TextProperty, nameTextBinding);
border.Child = tb;
this.Controllekes.Add(border);
它的作用是创建一个边框,并在该边框中创建一个应该发生绑定的文本块。我希望绑定属性 this.Dossier.Omschrijving (Dossier 是类)。如果我只是在文本框中输入一个字符串,它就可以工作。
在运行时生成边框但文本块保持为空。对象 Dossier.Omschrijving 包含信息。
我做错了什么?
编辑:
安全让我朝着正确的方向前进,ItemsControl with multiple DataTemplates for a viewmodel 的回答让我完成了工作:)
【问题讨论】:
标签: wpf data-binding wpf-controls mvvm-light