如何添加动态控制而不是静态控制?

发布于 2024-12-22 18:42:18 字数 1149 浏览 1 评论 0原文

我有一个包含字段城市名称、州名称和国家名称的集合,并将该集合绑定到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁许谁一生繁华 2024-12-29 18:42:18

您问题的关键是DataTemplates。这些允许您将视图绑定到自定义对象的集合。

您应该有一个公开 ObservableCollection 的 ViewModel,其中 TLocation 是公开公共属性 Cityname、Statename 和 Countryname 的类。

在您的视图中,您需要显示一个 ContentControl(例如 ListBox),将其 ItemSource 属性绑定到 ObservableCollection

然后,将列表框的 DataTemplate 设置为类似以下内容:

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding Path=CityName}" />
        <ComboBox Text="{Binding Path=StateName}" />
        <ComboBox Text="{Binding Path=CountryName}" />
    </StackPanel>
</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> 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文