2

我需要在代码隐藏中创建一个HierarchicalDataTemplatefor a 。TreeView

这就是我的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值?
4

2 回答 2

4

好的在我对您的问题的评论中,我指定了指定模板的方式背后的代码。现在,当我们将它们添加到 ResourceDictionaties 时,要使用/引用它们,我们必须使用 Key 添加它们。

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

而不是myWindow它可以是myParentPanel你的树视图的任何祖先。

但是有一个问题。。

密钥(即 DataTemplate)在设计时不存在。您正在运行时创建和添加它。

因此,如果您指的是这个数据模板,那么

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

    例如

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

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

希望这可以帮助。

于 2011-10-20T11:41:08.500 回答
0

我使用 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");
        }
    }

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

于 2021-01-08T11:59:02.083 回答