简体   繁体   English

手动将项目添加到TreeView时加载ItemTemplate?

[英]Load ItemTemplate when add items manually to a TreeView?

I'm creating my nodes (TreeViewItems) manually, I mean in code behind. 我手动创建我的节点(TreeViewItems),我的意思是代码背后。 I'm doing this because I'm loding subitems on demand when is expand and I have to add a dummy node. 我这样做是因为我在扩展时根据需要填充子项目,我必须添加一个虚拟节点。 That's the reason why I'm adding items manualy. 这就是为什么我要手动添加项目的原因。

Now, the big deal is when I create a TreeViewItem does not display the DataTemplate and show the default. 现在,最重要的是当我创建TreeViewItem时不显示DataTemplate并显示默认值。 The output window does not tell anything. 输出窗口没有任何说明。

        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);
        }

Here is my XAML Code: 这是我的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>

What can I do to display the DataTemplate? 我该怎么做才能显示DataTemplate?

I think the best option is to not add the objects directly to the TreeView Items property (as this will ignore DataTemplates) and create a Collection to add these items to and bind that to your TreeView , Then you can remove the x:Key property of your DataTemplate and use the DataType , this will apply the template to any objects of that type in the collection to your TreeView . 我认为最好的选择是不直接将对象添加到TreeView Items属性(因为这将忽略DataTemplates)并创建一个Collection以将这些项添加到TreeView并将其绑定到TreeView ,然后您可以删除x:Key属性您的DataTemplate并使用DataType ,这将模板应用于TreeView集合中该类型的任何对象。

Example: 例:

<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>

Code: 码:

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));
        }
    }
}

您忘记将树的ItemsTemplate设置为DataTemplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM