我对DataBinding没什么问题。 我想创建这样的结构:
Header Item ItemDetails Item ItemDetails Item ItemDetails Header Item ItemDetails Item ItemDetails
所以,我有自定义ItemTemplate的ListView:
<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之前,项目集合必须为空。
我该怎么做才能解决此错误?
答案 0 :(得分:4)
错误在于:
<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>
您正在尝试添加DataTemplate
作为ItemsControl
的孩子。显然,你想要做的是:
<ItemsControl Grid.Row="1" ItemsSource="{Binding Value, IsAsync=True}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding ItemName}"/>
<TextBlock Text="{Binding Description}" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
答案 1 :(得分:0)
使用ObservableCollection加载List项目。
即使您可以更改DataBound控件,但不建议这样做。而是使用ObservableCollection并使用您要加载的相应列表更改其内容。由于ObservableCollection可以轻松地将通知发送到绑定它的对象,因此UI列表将获得更新。