如何在代码隐藏中创建一个HierarchicalDataTemplate

本文关键字:一个 HierarchicalDataTemplate 创建 代码 隐藏 | 更新日期: 2023-09-27 18:11:53

我需要在代码隐藏中为TreeView创建一个HierarchicalDataTemplate

这是我的XAML看起来像:

<DataTemplate x:Key="DetailTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Height="15" Width="15" Source="{Binding Image}" Margin="0,0,5,0"/>
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </DataTemplate>
    <HierarchicalDataTemplate x:Key="MasterDetailTemplate" 
                              ItemsSource="{Binding SomeSource}" 
                              ItemTemplate="{StaticResource DetailTemplate}">
        <StackPanel Orientation="Horizontal">
            <Image Height="15" Width="15" Source="{Binding Image}" Margin="0,0,5,0"/>
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </HierarchicalDataTemplate>

这是我目前在c#中得到的:

        Image image = new Image();
        image.Name = "image";
        image.Height = 15;
        image.Width = 15;
        Binding imageBinding = new Binding("Image");
        BindingOperations.SetBinding(image, Image.SourceProperty, imageBinding);
        TextBlock textBlock = new TextBlock();
        textBlock.Name = "textBlock";
        Binding textBinding = new Binding("Text");
        BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, textBinding);
        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = Orientation.Horizontal;
        stackPanel.Children.Add(image);
        stackPanel.Children.Add(textBlock);
        DataTemplate dataTemplate = new DataTemplate();
        dataTemplate.DataTemplateKey

我被困在DataTemplateKey

  • 这是可能的吗?
  • 我从哪里去设置x:Key值?

如何在代码隐藏中创建一个HierarchicalDataTemplate

OK在我对你的问题的评论中,我指定了指定模板的方式后面的代码。现在,当我们将它们添加到resourcedictiones中时,为了使用/引用它们,我们必须使用key。

   myWindow.Resources.Add("MasterDetailTemplate", dataTemplate);

代替myWindow,它可以是myParentPanel,即任何祖先的树视图。

但是有一个问题……

Key(即DataTemplate)在设计时不存在。您在运行时创建并添加它。

如果你要引用这个数据模板,那么

  1. 将资源添加到资源字典后再引用。

    myWindow.Resources.Add(
         "MasterDetailTemplate",
         dataTemplate);
     myTreeView.ItemTemplate
       = myWindow.Resources["MasterDetailTemplate"] as HierarchicalDataTemplate;
    
  2. 在XAML中将动态创建的数据模板引用为DynamicResourceDynamicResource 消除了MasterDetailTemplate 在任何资源字典中预先存在的需要。

    <TreeView ItemTemplate="{DynamicResource MasterDetailTemplate}" ... >
      ....
    </TreeView>
    

我使用XAML中的资源:

<DataTemplate x:Key="TreeItemTemplate" DataType="{x:Type a:DriveStatusVar}">
 <StackPanel Orientation="Horizontal">
 <TextBlock  Text="{Binding PathName, NotifyOnSourceUpdated = True, NotifyOnTargetUpdated=True, Mode=TwoWay}" FontSize="10" Style="{StaticResource textBlockStyle}" IsEnabled="True"/>
 </StackPanel>
</DataTemplate>
<HierarchicalDataTemplate x:Key="TreeModTemplate" DataType="{x:Type a:ModuleGroup}" ItemsSource="{Binding Items}">
<StackPanel Orientation="Horizontal">
<Image  Source="{StaticResource add}"  Width="15" Height="15"></Image>
<TextBlock Text="{Binding Name, NotifyOnSourceUpdated = True, NotifyOnTargetUpdated=True,  Mode=TwoWay}" Style="{StaticResource textBlockStyle}" />
<TextBlock Text=" [" Foreground="Black" />
<TextBlock Text="{Binding Items.Count}" Foreground="Black" />
<TextBlock Text=" Items]" Foreground="Black" />
</StackPanel>
</HierarchicalDataTemplate>

并在定义和创建treeview对象的代码中使用它们:

TreeView tree = new TreeView(); 
HierarchicalDataTemplate hdt = (HierarchicalDataTemplate)this.Resources["TreeModTemplat"];
hdt.ItemTemplate = (DataTemplate)this.Resources["TreeItemTemplate"];                        
tree.ItemTemplate = hdt;
//add itemsource
 tree.ItemsSource = modList;

modList是一个带有列表项的ModuleGroup类的列表。Items是一个DriveStatusVar类的列表

 internal class ModuleGroup : INotifyPropertyChanged, ICloneable
{
    private bool _isSelected;
    private bool _isExpanded;
    private bool _isEdited;
    private string _name;
    private string _codesysName;
    private int _codesysId;
    private int _bits;
    private int _level;
    public  ObservableCollection<DriveStatusVar> Items { get; set; }
    public ObservableCollection<Alarm> Alarms { get; set; }
    public List<string> States { get; set; }
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            _isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }

等等…如果有人需要更多的代码,请告诉我!这只是其中的一部分

相关文章: