具有静态值的WPF组合框

本文关键字:WPF 组合 静态 | 更新日期: 2023-09-27 18:29:37

我需要为我的WPF应用程序分配几个静态组合框。我正在做以下事情。我收到错误内容无法识别?我如何才能最好地静态分配它们,在那里我可以获得selectedIndex和值,并相应地设置它们?

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

具有静态值的WPF组合框

您的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属性。

在用户向下滚动时添加无限年使用此代码,每当组合框的最后一年出现时,接下来的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();
    }