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>
같은 오류로
<TextBlock x:Name="quantity" Text="{Binding Measurement}" GotFocus="txt_GotFocus" />
에서 이벤트를 전환. TextBox 텍스트 바인딩의 경우, {Binding DataContext.MyProperty}와 {Binding MyProperty}를 시도했지만 어느 것도 작동하지 않습니다. – Echilon
컬렉션으로 시작합니다. get { "Binding Path = PropertyThatIsCollection"}에 디버그를 넣고 콜렉션이 호출되었는지 확인하십시오. TextBlock Path = 같은 바인딩은 상대적입니다. PresentationTrace High를 사용하여 더 많은 바인딩 오류를 얻을 수 있습니다. – Paparazzi