wpf - 如何使用 treeViewItems 的 dataTemplate
下面的代码抛出一个异常:(
<TreeView
ItemsSource="{Binding TreeRootInstance}"
x:Name="Htree"
ItemTemplate="CellTemplate"
SelectedItemChanged="HTree_OnSelectedItemChanged"
KeyDown="HTree_KeyDown">
<TreeView.Resources>
<DataTemplate x:Key="CellTemplate">
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ToggleButton x:Name="Expander"
HorizontalAlignment="Right"
Focusable="False"
Grid.Column="1"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type vw:TreeViewItem}}}"
ClickMode="Press"/>
<TextBlock
Text="{Binding Name}"
Grid.Column="0" />
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="False">
<Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<Style TargetType="{x:Type TreeViewItem}" x:Key="aa">
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="TabIndex" Value="0"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
<Setter Property="ItemsSource" Value="{Binding Children}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<StackPanel>
<Border Name="Bd">
</Border>
<ItemsPresenter x:Name="ItemsHost" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeView}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle" Value="{StaticResource aa}" />
</Style>
</TreeView.Resources>
</TreeView>
我省略了一些代码,但主要的雇佣关系仍然存在......) 该异常是 ItemsControl.ItemTemplate 上的 xaml 解析异常。
您能告诉我如何使这段代码工作吗?
谢谢, 李
The following code throws an exception:
<TreeView
ItemsSource="{Binding TreeRootInstance}"
x:Name="Htree"
ItemTemplate="CellTemplate"
SelectedItemChanged="HTree_OnSelectedItemChanged"
KeyDown="HTree_KeyDown">
<TreeView.Resources>
<DataTemplate x:Key="CellTemplate">
<Border>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<ToggleButton x:Name="Expander"
HorizontalAlignment="Right"
Focusable="False"
Grid.Column="1"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type vw:TreeViewItem}}}"
ClickMode="Press"/>
<TextBlock
Text="{Binding Name}"
Grid.Column="0" />
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="False">
<Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<Style TargetType="{x:Type TreeViewItem}" x:Key="aa">
<Setter Property="IsTabStop" Value="True"/>
<Setter Property="TabIndex" Value="0"/>
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
<Setter Property="ItemsSource" Value="{Binding Children}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeViewItem}">
<StackPanel>
<Border Name="Bd">
</Border>
<ItemsPresenter x:Name="ItemsHost" />
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost"
Property="Visibility"
Value="Collapsed"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeView}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle" Value="{StaticResource aa}" />
</Style>
</TreeView.Resources>
</TreeView>
(I've omitted some of the code but the main hirerchy is still there...)
The exception is a xaml parse exception, on ItemsControl.ItemTemplate.
Could you please tell me how to make this code work?
Thanks,
Li
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用纯字符串作为资源定位器,您需要使用 StaticResource 标记扩展,即
ItemTempate="{StaticResource CellTemplate}"
You can't use a plain string as a resource locator, you need to use either a StaticResource markup extension, i.e.,
ItemTempate="{StaticResource CellTemplate}"
ItemTemplate 行应为:
The ItemTemplate line should read: