开发者

wpf - how to use dataTemplate for treeViewItems

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>
                    &开发者_运维百科lt;/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


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}"


The ItemTemplate line should read:

ItemTemplate="{StaticResource CellTemplate}"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜