I am working on a project which has a nested listbox. The ItemsSource of inner-listbox referring to a collection of string defined in its parent listbox's ItemsSource. But when I run the code, I can't able to see the binded data in the nested listbox.
I searched the forum and found this and this solution but I wasn't able to resolve the problem yet. Maybe there is a problem with the code. So here is how the code looks like:
<ListBox ItemsSource="{Binding Items}" Name="detailList" Margin="5,5,0,0">
<ListBox.ItemTemplate >
<DataTemplate>
<StackPanel Orientation="Vertical" Width="90" Margin="5,0,0,0">
<TextBlock Text="{Binding Name}" Width="60" Height="30" TextWrapping="Wrap" FontSize="11" TextAlignment="Center"/>
<Expander Visibility="{Binding Visibility}" DockPanel.Dock="Top">
<ListBox ItemsSource="{Binding Path=SubFamiliesNames}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code Behind
public ObservableCollection<DirectoryItemViewModel> Items { get; set; }
public DefaultControl()
{
InitializeComponent();
Items = new ObservableCollection<DirectoryItemViewModel>
{
/*Some Initial Data*/
};
this.DataContext = Items;
}
DirectoryItemViewModel.cs
public class DirectoryItemViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
// Constructor
public DirectoryItemViewModel()
{
//initialize some data
_subfamiliesnames = new ObservableCollection<string>();
_subfamiliesnames.Add("File1");
_subfamiliesnames.Add("File2");
_subfamiliesnames.Add("File3");
}
#region Public Properties
public ObservableCollection<string> _subfamiliesnames;
public ObservableCollection<string> SubFamiliesNames
{
get { return _subfamiliesnames; }
set
{
_subfamiliesnames = value;
OnPropertyChanged("SubFamiliesNames");
}
}
// Full path of the directory
public string FullPath { get; set; }
// Item type (Enum)
public DirectoryItemType Type { get; set; }
public string Visibility
{
get
{
return "Visible";
}
}
//The name of the Directory
public string Name { get { return this.Type == DirectoryItemType.Drive ? this.FullPath : DirectoryStructure.GetFileFolderName(FullPath); } }
#endregion
}
Please share your solutions, would really appreciate the support. Thank you.