簡體   English   中英

手動將項目添加到TreeView時加載ItemTemplate?

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

我手動創建我的節點(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?

我認為最好的選擇是不直接將對象添加到TreeView Items屬性(因為這將忽略DataTemplates)並創建一個Collection以將這些項添加到TreeView並將其綁定到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));
        }
    }
}

您忘記將樹的ItemsTemplate設置為DataTemplate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM