WPF Treeview Icon Plus减号

时间:2013-11-12 12:57:50

标签: wpf treeview icons hierarchicaldatatemplate

我正在尝试在树视图中添加图标,但它没有显示出来。 HierarchicalDataTemplate位于Windows资源中,树视图位于网格中。

任何人都能告诉我我犯的错误是什么吗?

这是XML:

<Window x:Class="Treeview.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Support" Height="700" Width="1024" SizeToContent="WidthAndHeight" 
        mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        d:DesignHeight="229" Icon="/Test;component/Images/Treeview.jpg">

    <Window.Resources>

        <XmlDataProvider x:Key="questions" XPath="Questions/Question" />

        <HierarchicalDataTemplate x:Key="rootTemplate">

            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="child::*" />
            </HierarchicalDataTemplate.ItemsSource>
            <StackPanel Orientation="Horizontal">

                <CheckBox Name="checkBoxTree"  Checked="TreeView_Checked" Unchecked="checkBoxTree_Unchecked" />
                <Image Style="{StaticResource ExpandingImageStyle}">
                    <Image.Resources>
                        <BitmapImage x:Key="Icon_Closed" UriSource="Images/Plus.ico"/>
                        <BitmapImage x:Key="Icon_Open" UriSource="Images/Minus.ico"/>
                    </Image.Resources>
                </Image>
                <TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" />

                <TextBlock>
                            <Hyperlink NavigateUri="{Binding XPath=@WebSite}" RequestNavigate="Hyperlink_RequestNavigate">
                                <TextBlock Text="{Binding XPath=@WebSite}" /> 
                            </Hyperlink>    
                        </TextBlock>
            </StackPanel>

        </HierarchicalDataTemplate>

    </Window.Resources>
    <Grid>

            <TreeView Name="TreeViewer" HorizontalAlignment="Left" Height="220" Margin="10,116,0,0" Grid.Row="1" 
                      VerticalAlignment="Top" Width="700" Visibility="Collapsed"  FontSize="15" 
   ItemsSource="{Binding Source={StaticResource questions}}" ItemTemplate="{StaticResource rootTemplate}"  >

                <TreeView.Resources>


                    <Style x:Key="ExpandingImageStyle" TargetType="{x:Type Image}">
                        <Setter Property="Source" Value="{DynamicResource Icon_Closed}"/>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
                                <Setter Property="Source" Value="{DynamicResource Icon_Open}"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TreeView.Resources> 
            </TreeView>

    </Grid>

</Window>

1 个答案:

答案 0 :(得分:1)

当然,使用该XAML,Visual Studio错误列表中显示的警告如下所示?:

  

无法解析资源“ExpandingImageStyle”。

这告诉您,HierarchicalDataTemplate无法找到Resource。如果您将Style移到Window.Resources的顶部,则第一个问题应该会消失。但是,当您这样做时,您会收到警告,Style无法找到这两个BitmapImage Resources。因此,您最好将这两个Resources移到Window.Resources的顶部:

<BitmapImage x:Key="Icon_Closed" UriSource="Images/Plus.ico"/>
<BitmapImage x:Key="Icon_Open" UriSource="Images/minus.ico"/>
<Style x:Key="ExpandingImageStyle" TargetType="{x:Type Image}">
    ...
</Style>
<XmlDataProvider x:Key="questions" XPath="Questions/Question" />

<HierarchicalDataTemplate x:Key="rootTemplate">
    ...
        <Image Style="{StaticResource ExpandingImageStyle}" />
    ...
</HierarchicalDataTemplate>

如果您发现仍然无效,请查看您在Visual Studio中的Error ListOutput Window中可能遇到的任何错误或警告,并告诉我们。< / p>