2012-04-07 3 views
1

DataBox가 ListBox의 ItemTemplate 내에 중첩되어 있습니다. 이 사용하여 데이터 구조 같은 트리를 표시하려면 노력하고있어. 수업은 다음과 같습니다.ListBox의 중첩 된 Datagrid

내 데이터 컨텍스트의 개체에 Sections이라는 List<Section>이 포함되어 있으면 내 ListBox에 바인딩됩니다. 각 Section에는 Items이라는 List<Item>이 포함되어 있으며 eac ItemTemplate의 DataGrid는이 값에 바인딩됩니다.

응용 프로그램을 실행할 때 바인딩이있는 줄의 XAML에서 Null 참조 예외가 발생합니다. 이 작업을 수행하는 더 나은/대체 방법이 있습니까, 아니면 바인딩으로 트릭을 놓치고 있습니까?

<Window.Resources> 
    <CollectionViewSource x:Key="SectionSource" /><!-- this is initialized and filled with an ObservableCollection<Section> Sections when the window loads--> 
</Window.Resources> 

<ListBox x:Name="lstIngredients" ItemsSource="{Binding Source={StaticResource SectionSource}}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <DataTemplate.Resources> 
       <CollectionViewSource x:Key="itemsSource" Source="{Binding Items}"/> 
      </DataTemplate.Resources> 

<DataGrid x:Name="dgItems" IsReadOnly="false" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" 
    DataContext="{Binding}" 
    ItemsSource="{Binding Source={StaticResource Items}}" 
    EnableRowVirtualization="false" 
    VirtualizingStackPanel.VirtualizationMode="Standard" 
     <DataGrid.Columns> 
<DataGridTemplateColumn Width="2*" Header="{lex:LocText ChickenPing.Shared:Strings:Measurement}"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <TextBlock x:Name="quantity" Text="{Binding Measurement}" TextTrimming="CharacterEllipsis" TextAlignment="Left"/> 
        <!-- Null reference on this line caused by the binding. If I set this to any DependencyProperty on an Item object, I get a null reference--> 
       </DataTemplate> 

답변

0

DataContext를 행을 삭제합니다. 불행하게도

<Style x:Key="FocusableTextbox" TargetType="{x:Type TextBox}"> 
    <EventSetter Event="GotFocus" Handler="txt_GotFocus" /> 
</Style> 
... 
<TextBlock x:Name="quantity" Text="{Binding Measurement}" Style={StaticResource FocusableTextbox} /> 
0

이 필요 경로

ItemsSource="{Binding Source={StaticResource Items}}" 

ItemsSource="{Binding Path=PropertyThatIsCollection}" 

수 그리고 결국 TemplateColumns 중 하나에 설정 한 이벤트에이를 아래로 추적

+0

같은 오류로

<TextBlock x:Name="quantity" Text="{Binding Measurement}" GotFocus="txt_GotFocus" />

에서 이벤트를 전환. TextBox 텍스트 바인딩의 경우, {Binding DataContext.MyProperty}와 {Binding MyProperty}를 시도했지만 어느 것도 작동하지 않습니다. – Echilon

+0

컬렉션으로 시작합니다. get { "Binding Path = PropertyThatIsCollection"}에 디버그를 넣고 콜렉션이 호출되었는지 확인하십시오. TextBlock Path = 같은 바인딩은 상대적입니다. PresentationTrace High를 사용하여 더 많은 바인딩 오류를 얻을 수 있습니다. – Paparazzi

관련 문제