Xaml中如何对ComboBox绑定一个数据集合


Xaml代码如下:

< Window .Resources >
  
< local:MyData  x:Key ="list" />
 
</ Window.Resources >

 
< StackPanel >
  
< ComboBox  ItemsSource ="{Binding Source={StaticResource list}, Path=Values}"  IsSynchronizedWithCurrentItem ="True" />
 
</ StackPanel >

cs代码如下:
public   class  MyData
 
{
  
private ReadOnlyCollection<string> values;

  
public ReadOnlyCollection<string> Values
  
{
   
get return values; }
  }

 

  
public MyData()
  
{
   List
<string> myList = new List<string>();
   myList.Add(
"A");
   myList.Add(
"B");
   myList.Add(
"C");
   
this.values = new ReadOnlyCollection<string>(myList);
  }

 }

你可能感兴趣的:(combobox)