我试图在我的视图中使用selectedItem绑定属性(在我的视图模型中创建)。 我必须说列表框完全绑定到ObservableCollection并显示列表 但是当我尝试在messagebox上打印时,selectedItem我得到了空对象 这是我的ObservableCollection
ObservableCollection<Building> _buildingInstock = new ObservableCollection<Building>();
internal ObservableCollection<Building> BuildingInstock
{
get { return _buildingInstock; }
set
{
_buildingInstock = value;
if(PropertyChanged!=null)
{
PropertyChanged(this, new PropertyChangedEventArgs("BuildingInstock"));
}
}
}
这是xaml:
<ListBox x:Name="listview1" Grid.Row="11"
SelectedItem="{Binding Path=SelectedHouse,Mode=TwoWay}"
ItemTemplate="{StaticResource listboxtemplate}">
</ListBox>
在后面的视图代码中我有:
List_Of_Buildings temp=new List_Of_Buildings();
listview1.DataContext = temp.BuildingInstock;
listview1.ItemsSource = temp.BuildingInstock;
List_Of_Buildings类包含此属性:
Building _selectedHouse = new Building();
internal Building SelectedHouse
{
get { return _selectedHouse; }
set
{
_selectedHouse = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectedHouse"));
}
}