2013-10-30 59 views
2

我添加了這個控件,我如何動態地爲它分配標籤和內容?在Windows Store應用程序中動態填充組合框

<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

serversList.Items。??

例如,你可以添加文本框爲你的項目COMBOX

TextBlock tx = new TextBlock(); 
tx.Text = "something"; 
cbTst.Items.Add(tx);" 

,也可以刪除這樣一個項目: cbTst.Items.RemoveAt(該項目要刪除索引)

+0

這是組合框的只是內容,但問題是,我們如何與它綁定價值和內容。 – naCheex

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>