Setting ItemTemplate based on CheckBox value

Posted by ph0enix on Stack Overflow See other posts from Stack Overflow or by ph0enix
Published on 2010-04-08T03:59:29Z Indexed on 2010/04/08 4:03 UTC
Read the original article Hit count: 469

Filed under:
|
|

I have a DataTemplate which contains a CheckBox and ListBox. When the CheckBox is checked, I want to change the ItemTemplate property on the ListBox to change the appearance of each item.

Right now, it looks like this:

<DataTemplate DataType={x:Type MyViewModel}>
   <DockPanel>       
      <CheckBox DockPanel.Dock="Bottom"
                Content="Show Details"
                HorizontalAlignment="Right"
                IsChecked="{Binding ShowDetails}" 
                Margin="0 5 10 5" />

      <ListBox ItemsSource="{Binding Items}"
               ItemTemplate="{StaticResource SimpleItemTemplate}"
               Margin="10 0 10 5">
         <ListBox.Triggers>
            <DataTrigger Binding="{Binding ShowDetails}" Value="True">
               <Setter Property="ItemTemplate"
                       Value="{StaticResource DetailedItemTemplate}" />
            </DataTrigger>
         </ListBox.Triggers>
      </ListBox>
   </DockPanel>
</DataTemplate>

However, when I try to compile, I get the following error messages:

Value 'ItemTemplate' cannot be assigned to property 'Property'. Invalid PropertyDescriptor value.

and

Cannot find the static member 'ItemTemplateProperty' on the type 'ContentPresenter'.

I'm still fairly new to WPF, so perhaps there is something I'm not quite understanding?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about c#