2015-07-05 84 views
0

我剛開始ItemsControls /綁定多管閒事,我遇到了一個問題。我已經看過有關嵌套ItemsControls的各種教程,所以我不太確定我做錯了什麼。我相信所有代碼都可以正確編碼,但Expander不會顯示它應該顯示的內容。標題正確地將其自身對齊到其父項的頂部,但ScrollViewer不會出現,並且僅滾動父母「TimeScrollViewer」。我是否可能不正確地約束某些東西?WPF嵌套ItemsControls

所有的建議表示讚賞。

C#:

private string[][] hours = new string[][] 
{ 
    new string[] { "11:00", "11:30", "12:00", "12:30", "1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" }, 
    new string[] { "5:00", "5:30", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "9:00" } 
}; 

public class GuestItem 
{ 
    public string GuestName { get; set; } 
} 

public class RegistryItem 
{ 
    public string Header { get; set; } 
    public List<GuestItem> GuestList = new List<GuestItem>(); 
} 

Expander currentExpander = null; 

public MainWindow() 
{ 
    int day = (int)DateTime.Now.DayOfWeek; 

    InitializeComponent(); 

    List<RegistryItem> items = new List<RegistryItem>(); 

    foreach(string hour in hours[day]) 
    { 
     RegistryItem registryItem = new RegistryItem(){ Header = hour }; 

     registryItem.GuestList.Add(new GuestItem() { GuestName = "Bob" }); 
     registryItem.GuestList.Add(new GuestItem() { GuestName = "Frank" }); 
     registryItem.GuestList.Add(new GuestItem() { GuestName = "Jim" }); 

     items.Add(registryItem); 
    } 

    TimeItemsControl.ItemsSource = items; 
} 

private void ExpanderExpanded(object sender, RoutedEventArgs e) 
{ 
    if(currentExpander != null) 
    { 
     currentExpander.IsExpanded = false; 
    } 

    currentExpander = e.Source as Expander; 

    currentExpander.IsExpanded = true; 
} 

private void ExpanderCollapsed(object sender, EventArgs e) 
{ 
    currentExpander = null; 
} 

XAML:

<s:SurfaceScrollViewer Name="TimeScrollViewer" Grid.Row="1" Grid.Column="1" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden" Background="#4CAAAAFF" Style="{DynamicResource SurfaceScrollViewerHorizontalTop}" Foreground="#4CAAAAFF"> 
    <ItemsControl Name="TimeItemsControl"> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"/> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Expander Expanded="ExpanderExpanded" Collapsed="ExpanderCollapsed" Header="{Binding Header}" Style="{DynamicResource SurfaceExpander}" HorizontalContentAlignment="Center" FontSize="21.333" Width="100"> 
        <s:SurfaceScrollViewer Width="{Binding ElementName=TimeScrollViewer, Path=ActualWidth}" Height="{Binding ElementName=TimeScrollViewer, Path=ActualHeight}" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden"> 
         <ItemsControl ItemsSource="{Binding GuestList}"> 
          <ItemsControl.ItemsPanel> 
           <ItemsPanelTemplate> 
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left"/> 
           </ItemsPanelTemplate> 
          </ItemsControl.ItemsPanel> 
          <ItemsControl.ItemTemplate> 
           <DataTemplate> 
            <s:SurfaceButton Content="{Binding GuestName}"/> 
           </DataTemplate> 
          </ItemsControl.ItemTemplate> 
         </ItemsControl> 
        </s:SurfaceScrollViewer> 
       </Expander> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</s:SurfaceScrollViewer> 

回答

1

當你運行(調試),您的應用程序,並在Visual Studio中檢查輸出選項卡,可以看到下面的輸出多次:

System.Windows.Data Error: 40 : BindingExpression path error: 'GuestList' property not found on 'object' ''RegistryItem' (HashCode=15478206)'. BindingExpression:Path=GuestList; DataItem='RegistryItem' (HashCode=15478206); target element is 'ItemsControl' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

所以數據綁定到GuestList財產不能在012解析對象。如果您在類型的定義仔細觀察,你就會明白爲什麼:

public class RegistryItem 
{ 
    public string Header { get; set; } 
    public List<GuestItem> GuestList = new List<GuestItem>(); 
} 

GuestList不是財產,而是一個領域。 WPF綁定引擎需要屬性,所以GuestList字段儘管是公共的,但對於綁定引擎不存在,導致上述錯誤。要解決這個問題,只需將其設置爲屬性即可。您可以使用空的構造函數來初始化列表:

public class RegistryItem 
{ 
    public string Header { get; set; } 
    public List<GuestItem> GuestList { get; set; } 

    public RegistryItem() 
    { 
     GuestList = new List<GuestItem>(); 
    } 
} 

然後一切都會正常工作。所以底線是:總是檢查錯誤信息,特別是綁定錯誤,他們通常會告訴你什麼可能是錯誤的。由於綁定錯誤通常隱藏起來(因爲它們不是中的東西),您可以使用[在其他問題]( How can I turn binding errors into runtime exceptions?)中描述的技術將它們變爲完全異常或至少將它們記錄到其他地方。

+0

Yelp!這就是訣竅,非常感謝。從現在開始,我會密切關注這些錯誤! – Tannz0rz