У меня небольшая проблема с привязкой данных.Я хочу создать структуру следующим образом:
Header
Item ItemDetails
Item ItemDetails
Item ItemDetails
Header
Item ItemDetails
Item ItemDetails
Итак, у меня есть ListView с пользовательским ItemTemplate:
<ListView ItemsSource="{Binding Path=Data}">
<ListView.Template>
<ControlTemplate>
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
<Grid>
<ScrollViewer VerticalScrollBarVisibility="Hidden"
HorizontalScrollBarVisibility="Hidden"
x:Name="PART_AnimatedScrollViewer"
Padding="{TemplateBinding Padding}"
Focusable="false">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</ScrollViewer>
</Grid>
</Border>
</ControlTemplate>
</ListView.Template>
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Key}" Background="LightGray"/>
<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ItemName}"/>
<TextBlock Text="{Binding Description}" />
</Grid>
</DataTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Вот ViewModel:
public class ListViewModel : INotifyPropertyChanged
{
public ListViewModel()
{
_data.Add("ExampleHeader1", new List<MyObjectType>()
{
new MyObjectType("Example ItemName1", "Description1"),
new MyObjectType("Example ItemName2", "Description2"),
new MyObjectType("Example ItemName3", "Description3"),
});
_data.Add("ExampleHeader2", new List<MyObjectType>()
{
new MyObjectType("Example ItemName1", "Description1"),
new MyObjectType("Example ItemName2", "Description2"),
new MyObjectType("Example ItemName3", "Description3"),
new MyObjectType("Example ItemName4", "Description4"),
});
_data.Add("ExampleHeader3", new List<MyObjectType>()
{
new MyObjectType("Example ItemName1", "Description1"),
new MyObjectType("Example ItemName2", "Description2"),
new MyObjectType("Example ItemName3", "Description3"),
});
}
private readonly Dictionary<string, List<MyObjectType>> _data = new Dictionary<string, List<MyObjectType>>();
public IDictionary<string, List<MyObjectType>> Data
{
get { return this._data; }
}
private KeyValuePair<string, List<MyObjectType>>? selectedKey = null;
public KeyValuePair<string, List<MyObjectType>>? SelectedKey
{
get { return this.selectedKey; }
set
{
this.selectedKey = value;
this.OnPropertyChanged("SelectedKey");
this.OnPropertyChanged("SelectedValue");
}
}
public List<MyObjectType> SelectedValue
{
get
{
if (null == this.SelectedKey)
{
return new List<MyObjectType>();
}
return this._data[this.SelectedKey.Value.Key];
}
set
{
this._data[this.SelectedKey.Value.Key] = value;
this.OnPropertyChanged("SelectedValue");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propName)
{
var eh = this.PropertyChanged;
if (null != eh)
{
eh(this, new PropertyChangedEventArgs(propName));
}
}
}
Если я запустите это приложение, я получу ошибку:
Коллекция элементов должна быть пустой перед использованием ItemsSource.
Что мне нужно сделать, чтобы исправитьэта ошибка?