Search code examples
wpfxamllistboxdatatemplatedatatrigger

How to change ListBox DataTemplate in WPF based on CheckBox


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.


Solution

  • 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.