Hi,

I have to create some ContentControl code behind and be able to change their content and their DataTemplate according to their data type (image or text)

(So, I've created a ContentControl code behind, given it (code-behind too) a style from the XAML. In this style I've set DataTemplates)

I'm blocked with a binding problem! The 'person' object is displaying but I can't get to its 'display_text' property!

Could you please help? Thanks!!

PS: By the way, my goal is to create containers dynamically, then send them a 'person' object and have a picture displayed if available or a text displayed (if image not available)...so, if you have other suggestions on how to achieve this they all are welcome!! ;-)

Code:
<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2...l/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525" Background="#FF3A3A48">
    <Window.Resources>
        <local:RelativeToAbsolutePathConverter x:Key="relToAbsPathConverter" />
        <DataTemplate x:Key="ccTexte">
            <TextBlock Text="{Binding display_text}" HorizontalAlignment="Center" Foreground="Beige"/>
            <!--<TextBlock Text="{Binding Path=display_text}" HorizontalAlignment="Center" Foreground="Beige"/>-->
            <!--<TextBlock DataContext="{Binding DataContext,RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}" Text="{Binding Path=display_text}" HorizontalAlignment="Center" Foreground="Beige"/>-->
        </DataTemplate>
        <DataTemplate x:Key="ccImage">
            <Image x:Name="img" Source="{Binding display_image, Converter={StaticResource relToAbsPathConverter}}"/>
        </DataTemplate>
        <Style x:Name="ccStyle" TargetType="ContentControl">
            <Setter Property="ContentTemplate" Value="{StaticResource ccTexte}"/>
            <!--<Setter Property="ContentTemplate" Value="{StaticResource ccImage}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Source={StaticResource display_image}}" Value="{x:Null}">
                    <Setter Property="ContentTemplate" Value="{StaticResource ccTexte}"/>
                </DataTrigger>
            </Style.Triggers>-->
        </Style>        
    </Window.Resources>
    <Grid Name="mainGrid">
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
    </Grid>
</Window>
Code:
Imports System.IO
Class MainWindow
    Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        mainGrid.Children.Add(designCC(300, 200, "01"))
 
        Dim cc As ContentControl = LogicalTreeHelper.FindLogicalNode(mainGrid, "CC_01")
        cc.Content = New person With {.display_image = "01.png", .display_text = "Text 01"}
    End Sub
 
    Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim cc As ContentControl = LogicalTreeHelper.FindLogicalNode(mainGrid, "CC_01")
        cc.Content = New person With {.display_image = "02.png", .display_text = "Text 02"}
    End Sub
 
    Private Function designCC(width As Integer, height As Integer, id As String) As ContentControl
        Dim cc As New ContentControl
        cc.Width = width
        cc.Height = height
        cc.Name = "CC_" & id
        cc.Style = DirectCast(Me.Resources("ccStyle"), Style)
        Return cc
    End Function
End Class
 
Public Class person
    Private _display_image As String
    Public Property display_image() As String
        Get
            Return _display_image
        End Get
        Set(value As String)
            _display_image = value
        End Set
    End Property
    Private _display_text As String
    Public Property display_text() As String
        Get
            Return _display_text
        End Get
        Set(value As String)
            _display_text = value
        End Set
    End Property
End Class
 
Public Class RelativeToAbsolutePathConverter
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim relative As [String] = TryCast(value, String)
        If relative Is Nothing Or relative = "" Then
            Return Nothing
        End If
        Return System.AppDomain.CurrentDomain.BaseDirectory & "Images\" & relative
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class