2011-02-22 68 views
0

我對DataBinding幾乎沒有問題。 我想這樣的創建結構:WPF ListView綁定到IDictionary <字符串,IEnumerable <MyObjectType >>

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> 

這裏是視圖模型:

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)); 
     } 
    } 
} 

如果我要運行該應用程序我會得到錯誤:

Items collection must be empty before using ItemsSource.

我該如何解決這個錯誤?

回答

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一個孩子。顯然,你想要做的是這樣的:

​​
+0

謝謝=)我以爲我失去了小而重要的細節。 – Leonid 2011-02-22 21:07:00

+0

@Leonid - 發生這種情況,特別是在這麼晚的時候:) – 2011-02-22 21:12:34

0

使用ObservableCollection加載列表項。

即使您可以更改DataBound控件,但不推薦。而是使用ObservableCollection並使用您想要加載的適當列表更改其內容。由於ObservableCollection可以輕鬆地將通知發送到綁定的對象,因此您的UI列表將得到更新。

相關問題