【问题标题】:WPF combox with static values具有静态值的 WPF 组合框
【发布时间】:2016-01-11 15:43:08
【问题描述】:

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

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

【问题讨论】:

标签: c# wpf combobox


【解决方案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 属性。

【讨论】:

  • 我怎样才能为年组合框做同样的事情?也可以根据当前月份和年份预先选择它。?
  • 我想知道,你想要在那个组合框中多少年。因为如果有很多年,我建议你去数据绑定
  • 是的,我可能需要很多年说 10。那么最好的机制是什么
【解决方案2】:

在用户向下滚动时添加无限年份 使用此代码,每当看到组合框的最后一年时,接下来的 10 年将被添加到组合框并显示给用户。我希望这是你想要的。 RequestBringIntoView 事件在 XAML 组件进入视图时触发,即当它对用户可见时

   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();
    }

【讨论】:

  • 嗨,我不太明白这个功能 itemCombo_RequestBringIntoView?
  • RequestBringIntoView 每当文本框、文本块、组合框元素等 XAML 元素进入用户视图时即被触发,即用户可见....只需在组合框 XAML 中键入 RequestBringIntoView 并自动此事件将在后端创建...就像您编写点击事件的方式
猜你喜欢
  • 2023-03-17
  • 2020-03-23
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多