2016-03-16 3 views
0

Название говорит все этоСтиль DataTrigger не оценки на начальной загрузки

<Page.Resources> 
    <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/> 

    <DataTemplate x:Key="level1"> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/> 
      <TextBlock x:Name="GroupDescription" Text="{Binding description}"/> 
      <TextBlock x:Name="Depth" Text="{Binding childDepth}"/> 
      <ItemsControl ItemsSource="{Binding settings}"> 

      </ItemsControl> 
     </StackPanel> 
    </DataTemplate> 

    <DataTemplate x:Key="level2"> 
     <StackPanel Orientation="Vertical"> 
      <TextBlock x:Name="GroupTitle" FontSize="16" FontWeight="Bold" Foreground="#FF000532" Text="{Binding name}"/> 
      <TextBlock x:Name="GroupDescription" Text="{Binding description}"/> 
      <TextBlock x:Name="Depth" Text="{Binding childDepth}"/> 
      <ItemsControl ItemsSource="{Binding settings}"> 

      </ItemsControl> 
      <TabControl ItemsSource="{Binding groups}"> 
       <TabControl.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding name}" /> 
        </DataTemplate> 
       </TabControl.ItemTemplate> 
       <TabControl.ContentTemplate> 
        <DataTemplate> 
         <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource level1}"/> 
        </DataTemplate> 
       </TabControl.ContentTemplate> 
      </TabControl> 
     </StackPanel> 
    </DataTemplate> 
</Page.Resources> 
<Grid> 
    <ContentControl x:Name="SettingsDisplayer" Content="{Binding settingGroup, ElementName=page}"> 
     <ContentControl.Resources> 
      <local:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter"/> 
     </ContentControl.Resources> 
     <ContentControl.Style> 
      <Style TargetType="{x:Type ContentControl}"> 
       <Setter Property="ContentTemplate" Value="{StaticResource level1}" /> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding Path=childDepth}" Value="1"> 
         <Setter Property="ContentTemplate" Value="{StaticResource level1}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Path=childDepth}" Value="2"> 
         <Setter Property="ContentTemplate" Value="{StaticResource level2}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Path=childDepth}" Value="3"> 
         <Setter Property="ContentTemplate" Value="{StaticResource level3}" /> 
        </DataTrigger> 
        <DataTrigger Binding="{Binding Path=childDepth}" Value="4"> 
         <Setter Property="ContentTemplate" Value="{StaticResource level4}" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ContentControl.Style> 
    </ContentControl> 
</Grid> 

Поглотитель для childDepth даже и не обращались, потому что я поставил консольный след там, чтобы знать, когда она есть. Свойство заселено, так как оно показывает правильную сумму, если смотреть на глубину TextBlock в DataTemplate.

Соответствующее свойство в моем классе:

public int childDepth { get { Console.WriteLine(this.getDepth()); return this.getDepth(); } } 

Нет матер, что childDepth это Alwayse skipps спусковые и просто использует шаблон level1.

+0

Похоже, у вас есть файл XML с более чем один корневым тегом. XML обычно имеет только один корневой тег, поэтому ваш xml является фрагментом. Вы можете обернуть данные xml в один корневой тег, используя метод string(): ваш текущий xml. string myString = "" + xml + ""; – jdweng

+0

@jdweng Что? нет, я не хочу. Это даже не компилируется. – Wobbles

ответ

0

Я чувствую себя глупо, поэтому, по-видимому, связывание содержимого ContentControl не является эквивалентом данных привязки к ContentControl, поэтому мне нужно было нацелить мои привязки DataTrigger более конкретно на мой оригинальный DP, поскольку он не просто берет родительский контейнеров.

Это сработало:

<DataTrigger Binding="{Binding settingGroup.childDepth, ElementName=page}" Value="1"> 
    <Setter Property="ContentTemplate" Value="{StaticResource level1}" /> 
</DataTrigger>