views:

423

answers:

1

I am trying to apply a DataTrigger to change the DataTemplate for a listbox and am getting the error "Error 1 Cannot find the Trigger target 'IssueListBox'. (The target must appear before any Setters, Triggers, or Conditions that use it.)"

I have a Listbox in my Main Window (In a DockPanel along with other Controls):

<ListBox x:Name="IssueListBox"
  ItemsSource="{Binding}"
    ItemTemplate="{StaticResource ShowIssueSimple}" 
    IsSynchronizedWithCurrentItem="True"
    HorizontalContentAlignment="Stretch" 
    BorderThickness="3" DockPanel.Dock="Top" 
    VerticalContentAlignment="Stretch" Margin="2"/>

I have a pair of DataTemplates in App.xaml with a DataTrigger at the bottom of the 2nd Template:

    <DataTemplate x:Key="ShowIssueDetail">
        <Border CornerRadius="4, 8, 4, 8" Margin="2" MinWidth="400" BorderThickness="3" 
                BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}">
            <StackPanel Orientation="Horizontal">
                 <StackPanel Margin="10">
                    <TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/>
                    <StackPanel Width="Auto" Orientation="Horizontal">
                        <TextBlock Text="Due: " FontWeight="Bold"/>
                        <TextBlock Text="{Binding IssDueDate}" FontStyle="Italic" HorizontalAlignment="Left"/>
                    </StackPanel>
                    <StackPanel Width="Auto" Orientation="Horizontal">
                        <TextBlock Text="Category: " FontWeight="Bold"/>
                        <TextBlock Text="{Binding IssCategory}"/>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
        </Border>
    </DataTemplate>

    <DataTemplate x:Key="ShowIssueSimple">

        <Border CornerRadius="6" 
                Margin="2,1,2,1"
                MinWidth="400"
                BorderThickness="2" 
                SnapsToDevicePixels="True"
                BorderBrush="{Binding Path=IssUrgency, Converter={StaticResource IntToRYGBBoarderBrushConverter}}">
            <StackPanel Margin="5">
                <TextBlock Text="{Binding IssSubject}" FontWeight="Bold" FontSize="14"/>
            </StackPanel>
        </Border>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Source={StaticResource sbvm}, Path=ShowDetailListItems}" Value="True">
                <Setter TargetName="IssueListBox" Property="ItemTemplate" Value="{StaticResource ShowIssueDetail}"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

How do I get the Trigger to work? Mr Google has failed me, many examples like this abound but they are not based on another control.

+1  A: 

Your data template is a StaticResource defined in app.xaml, you are trying to do an element name binding to the element IssueListBox which doesnt exist in the same scope. Even then what you are trying to do is this. Listbox has a data template DT, inside DT you are trying to reach back to the List box and set its DataTemplate to another one (not DT).

Why dont you combine the templates, set the visibility on the details to collapsed and trigger the visibility based on your property. then you dont have to reference the list box at all and the template stays the same, it just changes internally when you want to see the details.

Aran Mulholland
That did the trick although I am still left wondering how I would handle it if I needed two templates. Life was so much easier when I just added an event handler to the checked.changed event but I am trying to develop better habits as I am learning adn make things as MVVMish as I can while learnign xaml.
Mike B
you can use a data template selector, but that is to decide between data templates when they are added, not when properties change.
Aran Mulholland