绑定转换器不适用于根TreeViewItem元素(WPF)

时间:2014-04-27 23:26:10

标签: c# wpf xaml treeview

我正在使用WPF提供的TreeView控件创建文件资源管理器应用程序。我已对其进行了自定义,以便在路径旁边显示一个文件图标。我使用IValueConverter将路径转换为所需的图片,我基于此页面:http://www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer-Tree

它主要起作用!除了根TreeViewItems由于某种原因不显示图标。我在IValueConverter :: Convert()函数上放置了一个断点,并确认它没有为根节点执行,但在此之后为所有子节点执行。

VS输出窗口没有显示绑定错误,因此我不知道如何发生这种情况。有什么想法吗?

转换器代码:

[ValueConversion(typeof(string), typeof(bool))]
public class HeaderToImageConverter : IValueConverter
{
    public static HeaderToImageConverter Instance = new HeaderToImageConverter();

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((value as string).Contains(@"\"))
        {
            Uri uri = new Uri("pack://application:,,,/Images/diskdrive.png");
            BitmapImage source = new BitmapImage(uri);
            return source;
        }
        else
        {
            Uri uri = new Uri("pack://application:,,,/Images/folder.png");
            BitmapImage source = new BitmapImage(uri);
            return source;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("Cannot convert back");
    }
}

TreeView XAML:

<TreeView Grid.Column="0" Name="fileExplorer" Margin="8,8,8,8">
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="20" Height="20" Stretch="Fill" Source="{Binding Converter={x:Static local:HeaderToImageConverter.Instance}}" />
                            <TextBlock Text="{Binding}" Margin="5,0" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。但除了TreeView.Resources我还有TreeView.ItemContainerStyle元素。将Style元素移动到那里实际上只设置了根,但不设置子项。因此,解决方案是将元素放在两个地方。

TreeView XAML:

<TreeView Grid.Column="0" Name="fileExplorer" Margin="8,8,8,8">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="20" Height="20" Stretch="Fill" Source="{Binding Converter={x:Static local:HeaderToImageConverter.Instance}}" />
                            <TextBlock Text="{Binding}" Margin="5,0" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Image Width="20" Height="20" Stretch="Fill" Source="{Binding Converter={x:Static local:HeaderToImageConverter.Instance}}" />
                            <TextBlock Text="{Binding}" Margin="5,0" VerticalAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>

现在,不必重用此HeaderTemplate Setter会更好。但我还没有办法做到这一点。