locked
WPF IMG Control Source property binding in PowerShell RRS feed

  • Question

  • Hi There,

    I've an wpf IMG control,which I want to set the img source dynamically through a custom property added to the treeviewItem :

    Here is the piece of code of IMG control.

    <TreeView Name="tviComp" >
            <TreeView.Resources>
                    <Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="HeaderTemplate">
                            <Setter.Value>
                                <DataTemplate DataType="ContentPresenter">
                                    <Grid>
                                        <StackPanel Name="spImg" Orientation="Horizontal">
                                            <Image  Name="imgNodeRoot"
                                                    Width="20"
                                                    Height="20"
                                                    Stretch="Fill"
                                                    Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}},Path=(nodeIcon)}"
                                                    VerticalAlignment="Center"
                                                    HorizontalAlignment="Left"
                                                    Grid.Column="1"
                                            />

    Here is the Powershell code that I create and add new property :

    $tk_tviItem=New-Object -TypeName System.Windows.Controls.TreeViewItem
    $tk_tviItem.Header="Computer"
    $tk_tviItem.Tag="C:\Temp\Folder.png"
    Add-Member -InputObject $tk_tviItem -MemberType NoteProperty -Name "nodeIcon" -Value "C:\Temp\Folder.png"

    In the wpf binding, if I use binding "Path=(Tag)" to bind to TAG property from treeview's own, the image is shown. But if I use binding "Path=(nodeIcon)" to bind to custom property that I added using ADD-MEMBER, the image is not shown.

    Anybody got an idea why binding to the own property works but it doesn't work on custom added property ?







    • Edited by T.S.K Thursday, July 27, 2017 11:25 PM
    Thursday, July 27, 2017 11:13 PM

Answers

All replies

  • The custom property only exists in the code context and not in the XAML rendering.  "bindings" that refer to elements of a XAML control have special binding code.  PowerShell has to do that all externally.

    Here is an article that explains how the TV works and how it uses icons and other elements.  Much of this code would need to be created in PowerShell.

    https://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode


    \_(ツ)_/

    • Marked as answer by T.S.K Thursday, July 27, 2017 11:53 PM
    Thursday, July 27, 2017 11:30 PM
  • Thanks for the reply. Yep, I've came around that one also. It seems that it is easier to implement that in C# rather than powershell. Anyway thanks for t he help ;)


    • Edited by T.S.K Friday, July 28, 2017 12:01 AM
    Friday, July 28, 2017 12:00 AM