image DataTemplate inside a contentcontrol WPF

3.9k views Asked by At

i have a DataTemplate

<DataTemplate x:Key="image">
  <Image x:Name="TheImage" />
     <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=OverallResult}">
                <DataTrigger.Value>
                    <local:resultType>Success</local:resultType>
                </DataTrigger.Value>
                <Setter TargetName="TheImage" Property="Source" Value="bin/Debug/Input/successx.jpg" />
            </DataTrigger>
 </DataTemplate>

with some trigger setters That Works fine in a GridView

  <ListView Margin="292,54,0,50" Name="listViewCaseSequence" MinHeight="215" Width="203" Button.Click="OnClick" ItemsSource="{Binding TestCaseSequenceList}" HorizontalAlignment="Left">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Result" CellTemplate="{StaticResource image}" Width="40"/>
      ...

now i would like to use it in some kind of StackPanel. I already found out that i could use a ContentControle

<StackPanel Orientation="Horizontal">
   <!-- doenst work --> <ContentControl ContentTemplate="{StaticResource image}" Content="{Binding OverallResult}" />
   <!-- works --> <TextBlock Text="{Binding OverallResult}" />
</StackPanel> 

The TextBlock works fine. But im missing something at the ContentControle thats doesnt let it render the image?

A pointer to the right reading source would be fine too :) Thanks in advance.

EDIT:

 ...
 <DataTrigger Binding="{Binding}">
 ...
 <ContentControl ContentTemplate="{StaticResource image}" Content="{Binding OverallResult}"/>
 ...

Output Says: System.Windows.Data Error: 40 : BindingExpression path error: 'OverallResult' property not found on 'object' ''resultType' (HashCode=0)'. BindingExpression:Path=OverallResult; DataItem='resultType' (HashCode=0); target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')

But why does he find the >OverallResult< on the Textblock thats works ?

EDIT2: Still not working

 ...
 <DataTrigger Binding="{Binding}">
 ...
 <ContentControl ContentTemplate="{StaticResource image}"/>
 ...

Edit3: Working:

<DataTrigger Binding="{Binding Path=OverallResult}">
<ContentControl ContentTemplate="{StaticResource image}" Content="{Binding}"/>
1

There are 1 answers

3
King King On BEST ANSWER

The DataContext in the DataTemplate here is in fact the Content you set for the ContentControl. Because it's already set to {Binding OverallResult}, so the Binding inside DataTemplate should be just {Binding} like this:

<DataTemplate x:Key="image">
   <Image x:Name="TheImage" />
   <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding}">
            <DataTrigger.Value>
                <local:resultType>Success</local:resultType>
            </DataTrigger.Value>
            <Setter TargetName="TheImage" Property="Source" 
                    Value="bin/Debug/Input/successx.jpg" />
        </DataTrigger>
   </DataTemplate.Triggers>
 </DataTemplate>

The above template should of course be used only for the StackPanel. For the ListView, just use the old DataTemplate.

However I think the Content you set for the ContentControl in this case can be just {Binding}, then the DataContext in both cases (for ListView and ContentControl) should be the same and we can use just one DataTemplate:

<ContentControl ContentTemplate="{StaticResource image}" Content="{Binding}"/>