Click here to Skip to main content
15,867,308 members
Home / Discussions / WPF
   

WPF

 
QuestionWPF Expander Pin
Itamar Gigi12-Sep-16 0:22
Itamar Gigi12-Sep-16 0:22 
AnswerRe: WPF Expander Pin
Pete O'Hanlon12-Sep-16 2:22
subeditorPete O'Hanlon12-Sep-16 2:22 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:36
Itamar Gigi12-Sep-16 2:36 
GeneralRe: WPF Expander Pin
Pete O'Hanlon12-Sep-16 2:58
subeditorPete O'Hanlon12-Sep-16 2:58 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 3:09
Itamar Gigi12-Sep-16 3:09 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:39
Itamar Gigi12-Sep-16 2:39 
GeneralRe: WPF Expander Pin
Itamar Gigi12-Sep-16 2:45
Itamar Gigi12-Sep-16 2:45 
QuestionDynamic ContentControl > Style > DataTemplate....binding problem! Pin
Jayme658-Sep-16 6:46
Jayme658-Sep-16 6:46 
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!! Wink | ;-)

XML
<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>


VB
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

QuestionListView SelectionChanged event not firing for the first already selected row. Pin
Stephen Holdorf7-Sep-16 7:40
Stephen Holdorf7-Sep-16 7:40 
AnswerRe: ListView SelectionChanged event not firing for the first already selected row. Pin
Stephen Holdorf9-Sep-16 5:52
Stephen Holdorf9-Sep-16 5:52 
QuestionWPF Listbox - How to keep the scrolling from changing the property? Pin
dbrenth6-Sep-16 3:49
dbrenth6-Sep-16 3:49 
QuestionDoes width and height need to be explicitly defined in ControlTemplate? Pin
Imagiv5-Sep-16 6:35
Imagiv5-Sep-16 6:35 
QuestionComboBox - SelectedValue and SelectedItem Pin
Mycroft Holmes31-Aug-16 14:48
professionalMycroft Holmes31-Aug-16 14:48 
QuestionWPF: Reading a document's bytes into a stream to store in a database table. Pin
Stephen Holdorf31-Aug-16 10:47
Stephen Holdorf31-Aug-16 10:47 
AnswerRe: WPF: Reading a document's bytes into a stream to store in a database table. Pin
Pete O'Hanlon31-Aug-16 11:16
subeditorPete O'Hanlon31-Aug-16 11:16 
QuestionUWP. How to find out size of uwp Windows.Storage element? Pin
wind te26-Aug-16 22:42
wind te26-Aug-16 22:42 
AnswerRe: UWP. How to find out size of uwp Windows.Storage element? Pin
Pete O'Hanlon31-Aug-16 12:29
subeditorPete O'Hanlon31-Aug-16 12:29 
QuestionClosing a static WPF Window from other Window instances. Pin
Stephen Holdorf23-Aug-16 3:23
Stephen Holdorf23-Aug-16 3:23 
AnswerRe: Closing a static WPF Window from other Window instances. Pin
Stephen Holdorf23-Aug-16 4:09
Stephen Holdorf23-Aug-16 4:09 
AnswerRe: Closing a static WPF Window from other Window instances. Pin
Richard Deeming23-Aug-16 4:27
mveRichard Deeming23-Aug-16 4:27 
QuestionCommunicating Between ViewModels Pin
JBHowl18-Aug-16 2:12
JBHowl18-Aug-16 2:12 
AnswerRe: Communicating Between ViewModels Pin
Pete O'Hanlon18-Aug-16 3:24
subeditorPete O'Hanlon18-Aug-16 3:24 
SuggestionRe: Communicating Between ViewModels Pin
AlexaGrey8628-Aug-16 2:43
AlexaGrey8628-Aug-16 2:43 
AnswerRe: Communicating Between ViewModels Pin
Kenneth Haugland18-Aug-16 5:07
mvaKenneth Haugland18-Aug-16 5:07 
QuestionWPF / Prism: Design decision with different views for same region Pin
SutterA17-Aug-16 2:12
SutterA17-Aug-16 2:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.