简体   繁体   English

如何使用XAML在C#应用程序中添加动态组合框

[英]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. 假设我要创建一个UI,在其中应从数据库动态显示组合框及其项(枚举值)。

suppose my sqlite database is like, 假设我的sqlite数据库是这样的,

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 之后,如何动态添加组合框,以便只有显示screen == 1的值才会显示其标签和枚举值

I am not sure how your data is coming from the database. 我不确定您的数据是如何从数据库中获取的。 But I know that ItemsControl is what you are looking for. 但是我知道ItemsControl是您要寻找的。 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. 我添加了示例绑定,您将必须根据数据结构和属性来更改绑定,具体取决于您使用的是MVVM还是后面的代码。 But ItemsControl should solve your problem. 但是ItemsControl应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将应用程序资源添加到应用程序的 C# 后端而不是 XAML 文件中? - How can I add application resources to my C# backend for the application instead of in the XAML file? 如何在XAML C#的动态组合框中仅显示字典的值部分? - How to display only the value part of my dictionary in a dynamic combobox for XAML C#? 我无法在C#应用程序上添加using引用 - I can't add a using reference on my C# application 如何在C#Combobox中添加动态文本框 - How to add dynamic textbox in C# Combobox 如何在我的XAML代码中将C#中的标签添加到网格中? - How can I add a label in C# to a grid in my XAML code? 如何在xaml和c#中为我的所有Windows中的所有背景添加更改颜色的按钮 - How can I add a button that changes color for all backgrounds in all my Windows in xaml and c# C#XAML-如何将组合框添加到某些datagrid ROWS而不添加到其他? - C# XAML - How to add a combobox to some datagrid ROWS but not others? 如何使用 C# 而不是 XAML 将两个 ResourceDictionary 对象合并到 Application.Current.Resources 中? - How can I merge two ResourceDictionary objects into Application.Current.Resources using C# instead of XAML? 如何在我的xaml项目中使用C#绘制画布? - How can i draw to canvas with C# in my xaml project? 如何在 C# 中使用 ComboBox 删除数据库中的数据? - How can I Delete Data in a Database Using ComboBox in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM