wpf - 如何使用 treeViewItems 的 dataTemplate

发布于 2024-11-27 12:59:55 字数 4183 浏览 1 评论 0原文

下面的代码抛出一个异常:(

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还给你自由 2024-12-04 12:59:55

您不能使用纯字符串作为资源定位器,您需要使用 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}"

白衬杉格子梦 2024-12-04 12:59:55

ItemTemplate 行应为:

ItemTemplate="{StaticResource CellTemplate}"

The ItemTemplate line should read:

ItemTemplate="{StaticResource CellTemplate}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文