如何添加动态控件而不是静态控件? [英] How to Add dynamic control instead of static control?
问题描述
我有一个包含城市名称,州名称和国家名称的集合,并将该集合绑定到我的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 = {绑定路径= CardField.FieldData,Mode = TwoWay} / gt;
<!-< ComboBox Margin = 10,0,0,0 x:Name = comboFieldData Grid.Column = 1 Text = {Binding Path = CardField.FieldTag} / >->
< / Grid>
< / Border>
问题的关键是 DataTemplates
。这些使您可以将视图绑定到自定义对象的集合。 code>,其中 TLocation
是一个公开公共属性城市名称,州名称和国家名称的类。
您的视图,您需要显示一个 ContentControl
,例如一个列表框,将它的 ItemSource
属性绑定到 ObservableCollection
。
然后将列表框的数据模板设置为:
< DataTemplate>
< StackPanel Orientation = Horizontal>
< TextBox Text = {Binding Path = CityName} />
< ComboBox Text = {Binding Path = StateName} />
< ComboBox Text = {Binding Path = CountryName} />
< / StackPanel>
< / DataTemplate>
另一种方法是使用DataGrid。请参阅本文
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>
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>
where TLocation
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's ItemSource
property bound to the ObservableCollection
.
Then you set the DataTemplate for the Listbox to something like:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=CityName}" />
<ComboBox Text="{Binding Path=StateName}" />
<ComboBox Text="{Binding Path=CountryName}" />
</StackPanel>
</DataTemplate>
Another approach is to use a DataGrid. See this article
这篇关于如何添加动态控件而不是静态控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!