[英]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.