我添加了这个控件如何动态地为它分配标签和内容?
<ComboBox x:Name="cbTst" SelectedValue="{Binding SelectedDirection, Mode=TwoWay}" SelectedValuePath="Tag" Canvas.Left="82" Canvas.Top="19">
<ComboBoxItem Content="North" Tag="0"/>
<ComboBoxItem Content="East" Tag="90"/>
<ComboBoxItem Content="South" Tag="180"/>
<ComboBoxItem Content="West" Tag="270"/>
</ComboBox>
答案 0 :(得分:4)
ComboBox cbTst = new ComboBox { Height = 30, Width = 100 };
cbTst.SelectedValue = "{Binding SelectedDirection, Mode=TwoWay}";
cbTst.SelectedValuePath = "Tag";
cbTst.Items.Add(new ComboBoxItem { Content = "North", Tag = 0 });
cbTst.Items.Add(new ComboBoxItem { Content = "East", Tag = 90 });
cbTst.Items.Add(new ComboBoxItem { Content = "South", Tag = 180 });
cbTst.Items.Add(new ComboBoxItem { Content = "West", Tag = 270 });
Gridx.Children.Add(cbTst);
XAML:
<Grid x:Name="Gridx" Margin="231,162,201,147">
</Grid>
答案 1 :(得分:0)
serversList.Items。??
例如,您可以将文本框添加为您的combox项目TextBlock tx = new TextBlock();
tx.Text = "something";
cbTst.Items.Add(tx);"
或者你可以删除这样的项目: cbTst.Items.RemoveAt(要删除的项目的索引)