我有以下类代码,如何使用绑定在组合框中显示默认值?
我在xaml中有一个coobobox。我想将它与这个类绑定。 下面的代码使组合框包含2个项目“任意”和“可配置”
但是,当我运行应用程序时,组合框是空的。 没有选择默认值。 如何做到这一点?
public class ReadData:INotifyPropertyChanged
{
private string typeData="Arbitrary";
private string[] typeDataList={"Arbitrary", "Configurable"};
private ICollectionView typeDataList;
public string[] TypeDataList
{
get
{
return typeDataList;
}
set
{
typeDataList=value;
NotifyProertyChanged("TypeDataList");
}
}
public string TypeData
{
get
{
return typeData;
}
set
{
typeData=value;
NotifyPropertyChanged("TypeData");
}
public ICollectionView TypeDataListView
{
get
{
typeDataListView=CollectionViewSource.GetDefaultView(typeDataList);
return typeDataListView;
}
set
{
typeDataListView=value;
//typeDataList= ???
}
}
XAML文件
<ComboBox ItemSource={Binding TypeDataListView}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding typeData}" Height="23" Width="120"/>
你的意思是我必须将我的字符串[]转换为ICollectionView?没有ICollectionView,这是不可能的?我使用.NET 4.我没有CollectionViewSource类。 – SHRI
.Net 4内置了CollectionViewSource。您可以保留字符串[],但您需要添加一个属性来绑定ListBox的SelectedItem。看到我的第一点。 –
对不起,是的。 .NET 4有CollectionViewSource类。我根据你的帖子更新。但是,仍然显示默认选择的项目。我在这里错过了什么吗? – SHRI