I'm currently creating a WPF application using MVVM. I have a large number of textboxes in a window (about 20) that need to be bound to specific elements in a list and need to be populated all at once. Normally I'd push them into an array and populate them that way but I can't do so without breaking the MVVM model. Is there a quick and efficient way I can do this while still adhering to MVVM?
我目前正在使用MVVM创建一个WPF应用程序。我在一个窗口中有大量的文本框(大约20个),它们需要绑定到列表中的特定元素,并且需要一次性填充。通常我会将它们推入一个数组并以这种方式填充它们,但我不能这样做而不破坏MVVM模型。是否有一种快速有效的方法可以在我仍然坚持使用MVVM的情况下实现这一点?
3 个解决方案
#1
2
I don't see why strictly this would break MVVM, if instead of using an Array you used a List, put it in your ViewModel and then use indexed binding to bind to specific elements.
我不明白这为什么会破坏MVVM,如果不是使用一个数组,而是使用一个列表,将它放到ViewModel中,然后使用索引绑定来绑定特定的元素。
Something like:
喜欢的东西:
<StackPanel>
<TextBox Text="{Binding MyViewModelList[0]}">
<TextBox Text="{Binding MyViewModelList[1]}">
<TextBox Text="{Binding MyViewModelList[2]}">
</StackPanel>
or if you want something more dynamic, instead of List, put an ObservableCollection in your VM, and bind to it in an ItemsControl with a DataTemplate.
或者,如果您想要更动态的东西,而不是列表,请在VM中放置一个ObservableCollection,并使用DataTemplate将其绑定到ItemsControl中。
<ItemsControl ItemsSource="{Binding Path=MyViewModelObsCol}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Syntax might not be 100% as I don't have an IDE to test, but something along these lines might be what you're after.
语法可能不是100%,因为我没有要测试的IDE,但是沿着这些线的东西可能就是您要的。
#2
5
You could bind your list to an ItemsControl
and change it's item template to be a TextBox
.
您可以将列表绑定到items控件,并将其项模板更改为文本框。
<ItemsControl ItemSource={Binding aList}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
#3
1
If you are trying to populate textbox on the bases of selection in list box try this Another option is creating a COllection view source which i don't think you will require here
如果你想要在列表框中选择的基础上填充文本框,请尝试另一个选项是创建一个集合视图源,我认为这里不需要这个
#1
2
I don't see why strictly this would break MVVM, if instead of using an Array you used a List, put it in your ViewModel and then use indexed binding to bind to specific elements.
我不明白这为什么会破坏MVVM,如果不是使用一个数组,而是使用一个列表,将它放到ViewModel中,然后使用索引绑定来绑定特定的元素。
Something like:
喜欢的东西:
<StackPanel>
<TextBox Text="{Binding MyViewModelList[0]}">
<TextBox Text="{Binding MyViewModelList[1]}">
<TextBox Text="{Binding MyViewModelList[2]}">
</StackPanel>
or if you want something more dynamic, instead of List, put an ObservableCollection in your VM, and bind to it in an ItemsControl with a DataTemplate.
或者,如果您想要更动态的东西,而不是列表,请在VM中放置一个ObservableCollection,并使用DataTemplate将其绑定到ItemsControl中。
<ItemsControl ItemsSource="{Binding Path=MyViewModelObsCol}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Syntax might not be 100% as I don't have an IDE to test, but something along these lines might be what you're after.
语法可能不是100%,因为我没有要测试的IDE,但是沿着这些线的东西可能就是您要的。
#2
5
You could bind your list to an ItemsControl
and change it's item template to be a TextBox
.
您可以将列表绑定到items控件,并将其项模板更改为文本框。
<ItemsControl ItemSource={Binding aList}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Text}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
#3
1
If you are trying to populate textbox on the bases of selection in list box try this Another option is creating a COllection view source which i don't think you will require here
如果你想要在列表框中选择的基础上填充文本框,请尝试另一个选项是创建一个集合视图源,我认为这里不需要这个