如何添加动态控制而不是静态控制?
我有一个包含字段城市名称、州名称和国家名称的集合,并将该集合绑定到我的 wpf 表单。我想在文本框中显示城市名称,在组合框中显示州名称,在组合框中显示国家名称。所有文本框和组合框都应该是动态的。我怎样才能做好这份工作?
任何人都建议我如何使用 MVVM 在 wpf 中动态设计此表单,我正在尝试执行此代码,但无法正确获得结果。要么我将所有内容都作为文本框或组合框,但我需要的是指定的文本框和组合框。
<Border Margin="3.5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125" />
<ColumnDefinition Width="*" MinWidth="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="tbFieldTag" Cursor="Hand" VerticalAlignment="Center" HorizontalAlignment="Stretch" TextWrapping="Wrap" Margin="10,0,0,0" Text="{Binding Path=CardField.FieldTag}" />
<TextBox Margin="10,0,0,0" x:Name="txtFieldData" Grid.Column="1" MaxLength="{Binding Path=CardField.MaximumLength}" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
<!--<ComboBox Margin="10,0,0,0" x:Name="comboFieldData" Grid.Column="1" Text="{Binding Path=CardField.FieldTag}"/>-->
</Grid>
</Border>
I have a collection with fields cityname, statename and countryname and I bind that collection to my wpf form. I want to display the cityname in a Textbox, the statename in a combobox and the countryname in a combobox. All the textboxes and comboboxes should come dynamically. How can I do this job?
Any one suggest me how to design this form dynamically in wpf using MVVM I am trying to do this code but not get result properly. Either I get everything as textbox or combobox, but what i need is textbox and combobox as specified.
<Border Margin="3.5">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="125" />
<ColumnDefinition Width="*" MinWidth="100" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock x:Name="tbFieldTag" Cursor="Hand" VerticalAlignment="Center" HorizontalAlignment="Stretch" TextWrapping="Wrap" Margin="10,0,0,0" Text="{Binding Path=CardField.FieldTag}" />
<TextBox Margin="10,0,0,0" x:Name="txtFieldData" Grid.Column="1" MaxLength="{Binding Path=CardField.MaximumLength}" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
<!--<ComboBox Margin="10,0,0,0" x:Name="comboFieldData" Grid.Column="1" Text="{Binding Path=CardField.FieldTag}"/>-->
</Grid>
</Border>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您问题的关键是
DataTemplates
。这些允许您将视图绑定到自定义对象的集合。您应该有一个公开
ObservableCollection
的 ViewModel,其中TLocation
是公开公共属性 Cityname、Statename 和 Countryname 的类。在您的视图中,您需要显示一个
ContentControl
(例如 ListBox),将其ItemSource
属性绑定到ObservableCollection
。然后,将列表框的 DataTemplate 设置为类似以下内容:
另一种方法是使用 DataGrid。请参阅本文
The key to your problem are
DataTemplates
. These allow you to bind your view to a collection of custom objects.You should have a ViewModel that is exposing an
ObservableCollection<TLocation>
whereTLocation
is a class that is exposing public properties Cityname, Statename and Countryname.In your View you need to show a
ContentControl
, say a ListBox, having it'sItemSource
property bound to theObservableCollection
.Then you set the DataTemplate for the Listbox to something like:
Another approach is to use a DataGrid. See this article