带有静态值的WPF combox

时间:2016-01-10 11:06:02

标签: c# wpf combobox

我需要为我的WPF应用程序分配一些静态组合框。我正在做如下。 我收到错误,内容无法识别?我如何最好地静态分配它们,我得到selectedIndex和值,并相应地设置它们?

<ComboBox Name="month" >
    <ComboBoxItem Tag="January" content="01" />
    <ComboBoxItem Tag="February" content="02" />
</ComboBox>

2 个答案:

答案 0 :(得分:1)

您的ComboBox身体中有ComboBoxItem。因此,必须首先将所选值类型化为ComboBoxItem。然后当它被转换时,您可以通过.value属性获取值。请参阅下面的代码。

在XAML中

<ComboBox x:Name="month">
   <ComboBoxItem Content="January"/>
   <ComboBoxItem Content="February"/>
   ...
</ComboBox>

在C#中

ComboBoxItem selectedItem = (ComboBoxItem)month.SelectedValue;
int index = month.SelectedIndex; //To get selected Index
int monthNumber = index + 1;
string itemvalue = selectedItem.Content;

您不需要Tag属性。

答案 1 :(得分:0)

在用户向下滚动时添加无限年份 使用此代码,每当组合框的去年出现时,未来10年将被添加到组合框并显示给用户。我希望这是你想要的。 只要XAML组件进入视图,即当用户可以看到

时,就会触发RequestBringIntoView事件
   public MainWindow()
    {
        InitializeComponent();
        PopulateNextTenYears();
    }



    private void PopulateNextTenYears()
    {
        //Check to see whether the year_combo is empty.
        //If yes, then simply add years from today to next 10 years
        if(year_combo.Items.Count==0)
        {
          for(int i=0;i<10;i++)
          {
              ComboBoxItem itemCombo = new ComboBoxItem()
              {
                  Content=""+(DateTime.Now.Year+i)
              };

              if(i==9)
              {
                  //If the combobox item is last, then register it to RequestBringIntoView event handler
                  itemCombo.RequestBringIntoView += itemCombo_RequestBringIntoView;
              }
              //Add the combobox item to year_combo
              year_combo.Items.Add(itemCombo);
          }
        }
            //If year_combo has some items i.e it is not empty, then extract the last year from combobox
            //using this year, add 10 more years
        else
        {
            ComboBoxItem itemCombo = (ComboBoxItem)year_combo.Items[year_combo.Items.Count - 1];
            int nextYear = Convert.ToInt32(itemCombo.Content)+1;
            for(int i=0;i<10;i++)
            {
                 itemCombo = new ComboBoxItem()
                {
                    Content = "" + (nextYear + i)
                };

                if (i == 9)
                {
                    //Again if the last item is added, then register it to RequestBringIntoView event
                    itemCombo.RequestBringIntoView += itemCombo_RequestBringIntoView;
                }
                year_combo.Items.Add(itemCombo);
            }
        }
    }

    void itemCombo_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
    {
        //Omce an event has been raised by a particular last combobox item, it is important to
        //unregister its RequestBringIntoView event
        ComboBoxItem itemCombo = (ComboBoxItem)sender;
        itemCombo.RequestBringIntoView -= itemCombo_RequestBringIntoView;
        //Populate next 10 years
        PopulateNextTenYears();
    }