简体   繁体   中英

How to display a control dynamically in wpf?

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

<UserControl.Resources>

<DataTemplate x:Key="IntegerTemplate">
    <DockPanel>
        <TextBox Margin="10,0,0,0" x:Name="IntegerTemplate" Grid.Column="1" MaxLength="{Binding Path=CardField.MaximumLength}" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
    </DockPanel>
</DataTemplate>

<DataTemplate x:Key="StringTemplate">
    <DockPanel>
        <ComboBox Margin="10,0,0,0" x:Name="cmbFieldData" Grid.Column="1" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
    </DockPanel>
</DataTemplate>
<DataTemplate x:Key="DefaultTemplate">
</DataTemplate>
<DataTemplate x:Key="dataTemplate">
    <ContentControl x:Name="MyContentControl"  Content="{Binding}" ContentTemplate="{StaticResource DefaultTemplate}"/>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=CardField.FieldTag}" Value="City">
                <Setter TargetName="MyContentControl" Property="ContentTemplate"
                   Value="{StaticResource IntegerTemplate}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=CardField.FieldTag}" Value="State">
                <Setter TargetName="MyContentControl" Property="ContentTemplate"
                   Value="{StaticResource StringTemplate}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=CardField.FieldTag}" Value="Country">
                <Setter TargetName="MyContentControl" Property="ContentTemplate"
                   Value="{StaticResource StringTemplate}" />
            </DataTrigger>
            <!-- and so on -->
        </DataTemplate.Triggers>
    </DataTemplate>
</UserControl.Resources>

we are using this code in our xaml page

<ItemsControl x:Name="items"
 ItemsSource="{Binding}"
 ItemTemplate="{StaticResource dataTemplate}"
/>

UPDATE: i am trying to do this following code:

<TextBlock x:Name="tbFieldTag" Cursor="Hand" VerticalAlignment="Center" HorizontalAlignment="Stretch" TextWrapping="Wrap" Margin="10,0,0,0" Text="{Binding Path=CardField.FieldTag}" />
            <ItemsControl x:Name="items"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource dataTemplate}"/>

In that i got Value of TextBlock but i am not getting the value in ItemTemplate. so where I doing wrong?

Try this:

1) data template selector

  public class CardFieldTemplateSelector : IValueConverter
    {

            public DataTemplate CityNameTemplate { get; set; } 

            public DataTemplate StateNameTemplate { get; set; }


            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string fieldTag = (string) value;
                switch (fieldTag)
                {
                    case "City":
                        return CityNameTemplate;
                    case "State":
                        return StateNameTemplate;
                }

                throw new ArgumentOutOfRangeException();
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                throw new NotSupportedException();
            }
        }

2) XAML:

 <selectors:CardFieldTemplateSelector x:Key="cardFieldTemplateSelector">
   <selectors:CardFieldTemplateSelector.CityNameTemplate>
     <DataTemplate>
      <DockPanel>
        <TextBox Margin="10,0,0,0" x:Name="IntegerTemplate" Grid.Column="1" MaxLength="{Binding Path=CardField.MaximumLength}" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
      </DockPanel>
     </DataTemplate>
   </selectors:CardFieldTemplateSelector.CityNameTemplate>

   <selectors:CardFieldTemplateSelector.StateNameTemplate>
     <DataTemplate>
      <DockPanel>
        <ComboBox Margin="10,0,0,0" x:Name="cmbFieldData" Grid.Column="1" Text="{Binding Path=CardField.FieldData, Mode=TwoWay}" />
     </DockPanel>
     </DataTemplate>
   </selectors:CardFieldTemplateSelector.StateNameTemplate>
 </selectors:CardFieldTemplateSelector>

<DataTemplate x:Key="dataTemplate">
   <ContentControl x:Name="MyContentControl" 
                   Content="{Binding}"
                   ContentTemplate="{Binding CardField.FieldTag, Converter={StaticResource cardFieldTemplateSelector}"/>
</DataTemplate>

<ItemsControl x:Name="items"
 ItemsSource="{Binding}"
 ItemTemplate="{StaticResource dataTemplate}"/>

Why can;t you use the WPF Data Form from Codeplex .

You can add custom editors in this form, based on the data types.

Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM