locked
The resource not found error RRS feed

  • Question

  •   <TreeView Name="treeView" ItemsSource="{Binding ConfigurationDescriptors}" SelectedItemChanged="TreeView_SelectedItemChanged" Loaded="treeView_Loaded">
               
                <TreeView.Resources>
                 
                    <HierarchicalDataTemplate DataType="{x:Type viewmodels:ContainerConfigurationItemViewModel}"
                                              ItemsSource="{Binding Path=Items}">
                        
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
    
                            <Image Source="{StaticResource ImageStyle}" SnapsToDevicePixels="True" />
                            <!--<Image Source="../resources/images/Expander_Closed.png" SnapsToDevicePixels="True"/>-->
                            <TextBlock Text="{Binding Path=Name}" Grid.Column="1" VerticalAlignment="Center" Margin="3,0,0,0" Foreground="Black"/>
                        </Grid>
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type viewmodels:ConfigurationElementItemViewModel}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="10"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding Path=Name}" Grid.Column="1" VerticalAlignment="Center" Margin="3,0,0,0" Foreground="Black">
                                <TextBlock.InputBindings>
                                    <MouseBinding MouseAction="LeftDoubleClick" 
                                                  Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TreeView}}, 
                                                  Path=DataContext.CurrentItem.ManageConfigurationCommand}"/>
                                </TextBlock.InputBindings>
                            </TextBlock>
                        </Grid>
                    </HierarchicalDataTemplate>
                    <Style x:Key="ImageStyle" TargetType="{x:Type Image}">
                        <Setter Property="Source" Value="../resources/images/Expander_Closed.png"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem},Path=IsExpanded, Mode=TwoWay}" Value="True">
                                <Setter Property="Source" Value="../resources/images/Expander.png"/>
                            </DataTrigger>                     
                        </Style.Triggers>
                    </Style>
                </TreeView.Resources>
            </TreeView>

    I am trying to have two different images on TreeView Open and Close. The resource is added.

    This Tree is embedded inside a Grid which in turn is inside a User Control.

    I tried adding the same resource inside the UserControl. It also didn't work.

    Here i found this error: An object of type System.Windows.Style can't be applied to a property that expects a system.windows.media.ImageSource





    • Edited by wpf_xam1 Saturday, November 14, 2015 5:16 AM
    Friday, November 13, 2015 9:03 PM

Answers

  • This is solved by:

      
    <Image>
                                <Image.Style>
                                    <Style TargetType="Image">
                                        <Setter Property="Source" Value="../resources/images/Expander_Closed.png"/>
                                        <Setter Property="SnapsToDevicePixels" Value="True"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
                                                <Setter Property="Source"  Value="../resources/images/Expander.png"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Image.Style>
      </Image>
    

    • Marked as answer by wpf_xam1 Saturday, November 14, 2015 5:21 AM
    Saturday, November 14, 2015 5:19 AM

All replies