0

我正在手动创建我的节点(TreeViewItems),我的意思是在后面的代码中。我这样做是因为我在展开时按需加载子项,并且我必须添加一个虚拟节点。这就是我手动添加项目的原因。

现在,最重要的是当我创建 TreeViewItem 时不显示 DataTemplate 并显示默认值。输出窗口什么也没说。

        DataTemplate dt1 = this.Resources["exerciseSetTemplate"] as DataTemplate;

        foreach (var qs in qss)
        {
            TreeViewItem tvi = new TreeViewItem();
            tvi.Header = qs.SetName;
            tvi.Tag = qs;
            tvi.ItemTemplate = dt1;
            tvi.Items.Add(yourDummyNode);

            treeView1.Items.Add(tvi);
        }

这是我的 XAML 代码:

<UserControl.Resources>
    <DataTemplate x:Key="questionSetTemplate">
        <StackPanel Orientation="Horizontal" Height="20" Margin="2,0,2,0">
            <Image Width="16" Height="16" Source="{StaticResource FolderImage}" Margin="0,0,5,0" />
            <TextBlock Text="{Binding SetName}" />
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <TreeView x:Name="treeView1" TreeViewItem.Expanded="treeView1_Expanded_1"
              ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    </TreeView>
</Grid>

我该怎么做才能显示 DataTemplate?

4

2 回答 2

2

我认为最好的选择是不要将对象直接添加到TreeView Items属性中(因为这将忽略 DataTemplates)并创建一个Collection以将这些项目添加到并将其绑定到您的TreeView,然后您可以删除您的x:Key属性DataTemplate并使用DataType,这将将模板应用于集合中该类型的任何对象到您的TreeView.

例子:

<UserControl x:Class="WpfApplication8.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication8"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="UI">
    <UserControl.Resources>
        <DataTemplate DataType="{x:Type local:MyObject}">
            <StackPanel Orientation="Horizontal" Height="20" Margin="2,0,2,0">
                <!--<Image Width="16" Height="16" Source="{StaticResource FolderImage}" Margin="0,0,5,0" />-->
                <TextBlock Text="{Binding SetName}" Foreground="Red"/>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources>

    <Grid DataContext="{Binding ElementName=UI}">
        <TreeView x:Name="treeView1" ItemsSource="{Binding TreeItems}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" />
    </Grid>
</UserControl>

代码:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        //  foreach (var qs in qss)
        for (int i = 0; i < 50; i++)
        {
            // TreeItems.Add(qs);
            TreeItems.Add(new MyObject { SetName = "Test1" }); 
        }
    } 

    private ObservableCollection<MyObject> _treeItems = new ObservableCollection<MyObject>();
    public ObservableCollection<MyObject> TreeItems
    {
        get { return _treeItems; }
        set { _treeItems = value; }
    }
}


// Your "qs" object
public class MyObject : INotifyPropertyChanged
{
    private string _folderImage;
    public string FolderImage
    {
        get { return _folderImage; }
        set { _folderImage = value; NotifyPropertyChanged("FolderImage"); }
    }

    private string _setname;
    public string SetName
    {
        get { return _setname; }
        set { _setname = value; NotifyPropertyChanged("SetName"); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}
于 2013-01-27T23:26:32.103 回答
0

您忘记将树设置ItemsTemplate为您的DataTemplate

于 2013-01-27T19:09:10.037 回答