C# 如何在代码隐藏中创建HierarchycalDataTemplate?
我需要为代码隐藏中的C# 如何在代码隐藏中创建HierarchycalDataTemplate?,c#,.net,wpf,datatemplate,hierarchicaldatatemplate,C#,.net,Wpf,Datatemplate,Hierarchicaldatatemplate,我需要为代码隐藏中的TreeView创建一个hierarchycaldatatemplate 这就是我的XAML的样子: <DataTemplate x:Key="DetailTemplate"> <StackPanel Orientation="Horizontal"> <Image Height="15" Width="15" Source="{Binding Image}" Margin="0,0,5,0"/>
TreeView
创建一个hierarchycaldatatemplate
这就是我的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>
我被数据模板键卡住了
- 这可以在代码隐藏中实现吗
- 从这里到哪里设置
值x:Key
myWindow.Resources.Add("MasterDetailTemplate", dataTemplate);
它可以是myParentPanel
而不是myWindow
,即树视图的任何祖先
但有一个问题
密钥(即数据模板)在设计时不存在。您正在运行时创建并添加它
因此,如果您指的是这个数据模板,那么
<TreeView ItemTemplate="{DynamicResource MasterDetailTemplate}" ... >
....
</TreeView>
e、 g
DynamicResource
DynamicSource
消除了在任何资源字典中预先存在MasterDetailTemplate
的需要
<TreeView ItemTemplate="{DynamicResource MasterDetailTemplate}" ... >
....
</TreeView>
....
希望这有帮助。好的,在我对您的问题的评论中,我指定了指定模板的代码隐藏方式。现在,要在将它们添加到ResourceDictionalies中时使用/引用它们,必须使用键添加它们
myWindow.Resources.Add("MasterDetailTemplate", dataTemplate);
它可以是myParentPanel
而不是myWindow
,即树视图的任何祖先
但有一个问题
密钥(即数据模板)在设计时不存在。您正在运行时创建并添加它
因此,如果您指的是这个数据模板,那么
<TreeView ItemTemplate="{DynamicResource MasterDetailTemplate}" ... >
....
</TreeView>
e、 g
DynamicResource
DynamicSource
消除了在任何资源字典中预先存在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>
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");
}
}
内部类模块组:INotifyPropertyChanged,iClonable
{
私立学校当选;
私人住宅被扩展;
私人编辑部;
私有字符串\u名称;
私有字符串_codesyname;
私有int_codesysId;
私有整数位;
私人国际级;
公共ObservableCollection项{get;set;}
公共ObservableCollection报警{get;set;}
公共列表状态{get;set;}
公选学校
{
获取{return}isSelected;}
设置
{
_isSelected=值;
不动产变更(“IsSelected”);
}
}
等等……如果有人需要更多的代码,请告诉我!这只是其中的一部分。我使用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>
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");
}
}
内部类模块组:INotifyPropertyChanged,iClonable
{
私立学校当选;
私人住宅被扩展;
私人编辑部;
私有字符串\u名称;
私有字符串_codesyname;
私有int_codesysId;
私有整数位;
私人国际级;
公共ObservableCollection项{get;set;}
公共ObservableCollection报警{get;set;}
公共列表状态{get;set;}
公选学校
{
获取{return}isSelected;}
设置
{
_isSelected=值;
不动产变更(“IsSelected”);
}
}
等等……如果有人需要更多的代码,请告诉我!这只是其中的一部分。,@AngelWPF我会看一看。@AngelWPF仍然不能解决我的
x:Key
问题。我不能设置Key
,当涉及到HierarchycalDataTemplate
,我不能设置任何绑定…请看下面的答案。@AngelWPF我会的我看一看。@AngelWPF仍然无法解决我的x:Key
问题。我无法设置Key
,当涉及到层次数据模板时,我无法设置任何绑定…请参见下面我的答案。