Xaml代码如下:
<Window.Resources>
<local:MyData x:Key="list"/>
</Window.Resources>
<StackPanel>
<ComboBox ItemsSource="{Binding Source={StaticResource list}, Path=Values}" IsSynchronizedWithCurrentItem="True"/>
</StackPanel>
<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);
}
}
{
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);
}
}