简体   繁体   中英

How can I add Dynamic combobox in my c# application using xaml

Suppose I want to create UI in which It should display combobox & its items(enum values)dynamically from the database.

suppose my sqlite database is like,

Name           datatype          Values                    IsWizard    Screen
Application     enum       0:first,1:second,3:third           1          1
Demo            enum         0:Hello 1:bye                    1          0

This is code I wrote,

for (int iCount = 0; iCount < ParameterCollection.Count; iCount++)
            {

                objIParameter = ParameterCollection.ElementAt(iCount).Value as IParameter;

                objIParameter.GetColumnValue("Iswizard", out iswizard);
                objIParameter.GetColumnValue("Screen", out screen);
                if (iswizard == 1 && screen == 1)
                {
                    WizardCollection.Add(ParameterCollection.ElementAt(iCount).Key, objIParameter);


                    objIParameter.GetColumnValue(DBEnumName, out enumValues);

                     string[] enumval = enumValues.Split(',');

After this how can I add combobox dynamically so that only the values who is having screen ==1 will display with its label and enumvalues inside the combobox

I am not sure how your data is coming from the database. But I know that ItemsControl is what you are looking for. Which goes something like this:

<Window.Resources>
    <DataTemplate x:Key="ItemTemplate">
        <Border BorderBrush="Black" BorderThickness="0.5" Margin="2">
            <Grid Margin="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*" />
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="1*" />
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" Text="{Binding Path=Label}" />
                <ComboBox Grid.Column="2"
                      ItemsSource="{Binding Path=EnumCollectionName}"
                      DisplayMemberPath="Name"
                      SelectedValuePath="Value" />
            </Grid>
        </Border>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ItemsControl ItemsSource="{Binding Path=SampleItems}"
              ItemTemplate="{StaticResource ItemTemplate}"
              Margin="10" />
</Grid>

I have added sample bindings, you will have to change the bindings as per your data structures and properties depending upon whether you are using MVVM or code behind. But ItemsControl should solve your problem.

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