In WPF, Selecting ItemContainerStyle based on data bound content

Posted by Bart Roozendaal on Stack Overflow See other posts from Stack Overflow or by Bart Roozendaal
Published on 2009-09-21T15:27:11Z Indexed on 2010/03/13 13:05 UTC
Read the original article Hit count: 586

Filed under:
|

In #WPF you have ItemTemplateSelectors. But, can you also select an ItemContainerStyle based on the datatype of a bound object?

I am databinding a scatterview. I want to set some properties of the generated ScatterViewItems based on the object in their DataContext. A mechanism similar to ItemTemplateSelector for styles would be great. Is that at all possible? I am now binding to properties in the objects that I am displaying to get the effect, but that feels like overhead and too complex (and most importantly, something that our XU designers can't do by themselves).

This is the XAML that I am using now. Your help is greatly appreciated.

      <s:ScatterView x:Name="topicsViewer">
   <s:ScatterView.ItemTemplateSelector>
    <local:TopicViewerDataTemplateSelector>
     <DataTemplate DataType="{x:Type mvc:S7VideoTopic}">
      <Grid>
       <ContentPresenter Content="{Binding MediaElement}"  />
       <s:SurfaceButton Visibility="{Binding MailToVisible}" x:Name="mailto" Tag="{Binding Titel}" Click="mailto_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" Width="62" Height="36">
        <Image Source="/Resources/MailTo.png" />
       </s:SurfaceButton>
       <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom" HorizontalAlignment="Center" Height="32">
        <s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnPlay" Click="btnPlay_Click">
         <Image Source="/Resources/control_play.png" />
        </s:SurfaceButton>
        <s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnPause" Click="btnPause_Click">
         <Image Source="/Resources/control_pause.png" />
        </s:SurfaceButton>
        <s:SurfaceButton Tag="{Binding MediaElement}" x:Name="btnStop" Click="btnStop_Click">
         <Image Source="/Resources/control_stop.png" />
        </s:SurfaceButton>
       </StackPanel>
      </Grid>
     </DataTemplate>
     <DataTemplate DataType="{x:Type mvc:S7ImageTopic}">
      <Grid>
       <ContentPresenter Content="{Binding Resource}" />
       <s:SurfaceButton Visibility="{Binding MailToVisible}" x:Name="mailto" Tag="{Binding Titel}" Click="mailto_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent" Width="62" Height="36">
        <Image Source="/Resources/MailTo.png" />
       </s:SurfaceButton>
      </Grid>
     </DataTemplate>
     <DataTemplate DataType="{x:Type local:Kassa}">
      <ContentPresenter Content="{Binding}" Width="300" Height="355" />
     </DataTemplate>
    </local:TopicViewerDataTemplateSelector>
   </s:ScatterView.ItemTemplateSelector>
   <s:ScatterView.ItemContainerStyle>
    <Style TargetType="s:ScatterViewItem">
     <Setter Property="MinWidth" Value="200" />
     <Setter Property="MinHeight" Value="150" />
     <Setter Property="MaxWidth" Value="800" />
     <Setter Property="MaxHeight" Value="700" />
     <Setter Property="Width" Value="{Binding DefaultWidth}" />
     <Setter Property="Height" Value="{Binding DefaultHeight}" />
     <Setter Property="s:ScatterViewItem.CanMove" Value="{Binding CanMove}" />
     <Setter Property="s:ScatterViewItem.CanScale" Value="{Binding CanScale}" />
     <Setter Property="s:ScatterViewItem.CanRotate" Value="{Binding CanRotate}" />
     <Setter Property="Background" Value="Transparent" />
    </Style>
   </s:ScatterView.ItemContainerStyle>
  </s:ScatterView>

Bart Roozendaal, Sevensteps

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding