0

중첩 된 데이터 템플릿 (ListViewItems)에서 호출되는 IValueConverters에서 문제가 발생합니다.중첩 된 데이터 템플릿 내에서 값 변환기를 참조하는 wpf (null 참조 예외가 발생 함)

나는

모두는 깊은 중첩 수준에 IValueConverter 구현을 제외하고 잘 작동합니다 ... 복잡한 (목록) 객체의 목록 (너무 많은 소스 코드를 여기에 다 넣어)의 다른 목적 및 목록을 포함하는 객체가 ...

Simplifyed 짧아 XAML :

<Window x:Class="XXXX.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:converter="clr-namespace:XXXX.Converter" 
    Width="800" 
    Height="600" 
    Icon="/Images/Icons/Calendar.ico" 
    Loaded="Window_Loaded" 
    Style="{StaticResource WindowDefaultStyle}" 
    WindowState="Maximized"> 
<Window.Resources> 
    <!-- <converter:ValueToVisibilityConverter x:Key="ValueToVisibility" /> --> 
    <converter:PercentageConverter x:Key="PercentFromValue" /> 
    <converter:SubtractionConverter x:Key="SubstractFromValue" /> 
    <converter:SingleTextLineConverter x:Key="inSingleLine" /> 
    <converter:B2VConverter x:Key="B2V" /> 
</Window.Resources> 

    <Grid Name="grid_Supplier"> 
     <ListView Name="listview_Product" 
      Grid.Row="1" 
      Height="{Binding ElementName=grid_Supplier, 
      Path=ActualHeight, 
      Converter={StaticResource SubstractFromValue}, 
      ConverterParameter=60}" 
      ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
      AncestorType=Window},Path=SuppliersProducts}"> 
       <ListView.ItemTemplate> 
        <DataTemplate> 
         <ListView Name="listview_CM" 
           Width="{Binding ElementName=grid_SupplierProduct, 
           Path=ActualWidth, 
           Converter={StaticResource PercentFromValue}, 
           ConverterParameter=80}" 
           ItemsSource="{Binding CM}"> 
           <ListView.ItemTemplate> 
            <DataTemplate> 
             <ListView Name="listview_Comments" 
              ItemsSource="{Binding Comments}"> 
              <ListView.ItemTemplate> 
               <DataTemplate> 
                <TextBlock 
                  Text="{Binding Comment.DueDate, StringFormat='dd.MM.yyyy'}" 
                  Background="{Binding Status 

, 컨버터 = {} 정적 리소스 B2V

}"/> 
               </DataTemplate> 
              </ListView.ItemTemplate> 
             </ListView> 
            </DataTemplate> 
           </ListView.ItemTemplate> 
         </ListView> 
        </DataTemplate> 
       </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 
</Window> 

당신은 3 개 중첩의 ListView, "첫 번째"레벨 변환기 (PercentFromValue) 문제없이 작동하고있다 볼 수있는 - "바인딩 및 매개 변수 ...

내 문제는 다음 중첩에있다 (null) 참조 예외가 런타임에 발생하면 (더 이상의 설명이나 중첩 예외 정보를 사용할 수 없음 ...) 변환기가 사라지 자마자 아무런 문제가되지 않습니다 ... OK 예외는 없지만 또한 Background - 예상대로 ...)

"li stview 리소스 "부분을 있지만 또한 Null 참조 예외가 상승 ("변환기 : "경로의 일부를 찾을 수없는 경우 가정합니다.

Backgroundworker (gui에 표시된 정보가 9 개 이상의 관련 테이블에서 수집 됨)를 사용하고 있기 때문에 Dispatcher에서 상속되고 DependencySource가 다음과 같은 이유로 생성됩니다. DependencyProperty "오류). 이제

나는 ... 백그라운드에서 모든 것을 준비하고 해결 방법과 동일한 스레드에서 단지 배경 - 브러쉬를 추가,하지만 못생긴하고

변환기를 사용하는 것이 훨씬 더 나은,하지만 방법 것입니다 중첩 된 데이터 템플릿 내에서이를 참조 할 수 있습니까?

도움을 주셔서 감사합니다.

답변

1

나는 ... 따라서 "플랫"구조는 직접 더 이상 중첩되지 창조하는 Window.Resources에 DataTemplates를 이동하고 서로 refererence 만들기와 그것을

<Window x:Class="XXXX.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:converter="clr-namespace:XXXX.Converter" 
    Width="800" 
    Height="600" 
    Icon="/Images/Icons/Calendar.ico" 
    Loaded="Window_Loaded" 
    Style="{StaticResource WindowDefaultStyle}" 
    WindowState="Maximized"> 
<Window.Resources> 
    <converter:PercentageConverter x:Key="PercentFromValue" /> 
    <converter:SubtractionConverter x:Key="SubstractFromValue" /> 
    <converter:SingleTextLineConverter x:Key="inSingleLine" /> 
    <converter:B2VConverter x:Key="B2V" /> 

    <DataTemplate x:Key="CommentTemplate"> 
<TextBlock Text="{Binding Comment.DueDate, StringFormat='dd.MM.yyyy'}" Background="{Binding Status, Converter={StaticResource B2V}}" 
    </DataTemplate> 

    <DataTemplate x:Key="CommentsTemplate"> 
<ListView Name="listview_Comments" 
      ItemsSource="{Binding Comments}" 
      ItemTemplate={StaticResource CommentTemplate} /> 
    </DataTemplate> 


    <DataTemplate x:Key="CMTemplate"> 
     <ListView Name="listview_CM" 
     Width="{Binding ElementName=grid_SupplierProduct, 
     Path=ActualWidth, 
     Converter={StaticResource PercentFromValue}, 
     ConverterParameter=80}" 
     ItemsSource="{Binding CM}" 
     ItemTemplate={StaticResource CommentsTemplate} /> 
    </DataTemplate> 

</Window.Resources> 

<Grid Name="grid_Supplier"> 
    <ListView Name="listview_Product" 
     Grid.Row="1" 
     Height="{Binding ElementName=grid_Supplier, 
     Path=ActualHeight, Converter={StaticResource SubstractFromValue}, 
     ConverterParameter=60}" 
     ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
     AncestorType=Window},Path=SuppliersProducts}" 
     ItemTemplate={StaticResource CMTemplate}/> 
</Grid> 

테이크를 자신을 해결했다 가장 안쪽의 템플리트가 첫 번째로 정의된다는 점에 유의하십시오. 그렇지 않으면 템플릿을 찾을 수 없습니다.

파일의 구조화로 인해 우리는 한 수준에서 모든 "평면"을 가지며 변환기가 발견되고 참조됩니다.