ListBox项是isEnabled WP7

时间:2012-02-24 15:38:59

标签: c# winforms windows-phone-7

我正在尝试将ListBox项的isEnabled属性设置为false。代码中包含一些xaml,它展示了如何在设计器中完成它,但我需要在代码中完成它。

这是我的代码:

string[] names = { "alpha", "beta", "gamma", "delta" };

        for (int i = 0; i < names.Length; i++)
        {
            listBox1.Items.Add(names[i].ToString());

            //set items 2 & 4 to isEnabled=false
             //   <ListBoxItem Content="beta" IsEnabled="False" />  xaml code

            // My Attempt, does not compile, cannot be used like a method
            // listBox1.isEnabled(2,false);  


        }

这适用于使用C#/ Silverlight的WindowsPhone7应用程序。

2 个答案:

答案 0 :(得分:-1)

怎么样:

 listBox1.Items.Add(new ListBoxItem() { Content = "one", IsEnabled = true });
 listBox1.Items.Add(new ListBoxItem() { Content = "two", IsEnabled = false});

然后,当您想要获取所选项目时,您可以

  void listbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
  {
       ListBoxItem selectedItem = listbox1.SelectedItem;
       stirng content = selectedItem.Content.ToString()
  }

答案 1 :(得分:-1)

如果您想为第三项设置它,例如:

listBox1.Items[2].IsEnabled = false;