2012-07-17 76 views
0

我有一个包含组合框的c#WPF项目。 我有几个项目在“选择”,如果我选择其中的任何一个,那么值是正确的(第一个项目为0,第二个项目为1,因此第四个) 我如何将第一个项目添加为“all “,如果用户选择”全部“,那么值是”-1“? 现在“所有”是第一项,因此,将其设置为“0”值Combobox selecteditem如果默认的“所有”值应该是-1

感谢的人谁使用可以帮助,没有什么选择尝试

+0

你永远不会具有指数= -1获得的项目。所选索引为-1意味着没有项目被选中。只需将您的ComboBox保持原样,使用索引0处的“all”项,然后测试选定的索引= 0。 – 2012-07-17 14:04:08

回答

1

值-1状态的SelectedIndex-1作为你的价值。如果您参考xaml中的SelectedIndex,则需要一些converter的绑定。

1

而不是使用SelectedIndex使用SelectedItem并检查它的“All”值。

例子:

视图模型

public class MyViewModel 
{ 
    public ObservableCollection<string> TheItems { get; set;} 
    public string TheSelectedItem { get; set; } 

    public MyViewModel() 
    { 
     TheItems = new ObservableCollection<string>(); 
     TheItems.Add("All"); 
     //Adding all the other value 

    } 

    public void SomeMethod() 
    { 
     if(TheSelectedItem=="All") 
     { 
      //Do whatever needs to be done 
     } 
    } 
} 

查看

<ListView ItemsSource="{Binding TheItems}" SelectedItem="{Binding TheSelectedItem}"/> 
+0

如果它是“全部”价值?我仍在学习,也许你有我需要使用的代码?等 – 2012-07-17 13:27:15

+0

我编辑了我的答案 – 2012-07-17 13:52:41

相关问题