简体   繁体   English

在TreeView HierarchicalDataTemplate中使用样式

[英]Use Style in TreeView HierarchicalDataTemplate

I'm new to this and can't quit get the correct syntax. 我对此并不陌生,无法退出以获取正确的语法。 This works correctly to capture the Left Mouse click on the textbox within the treeview: 这可以正确捕获树视图内文本框上的Left Mouse click

<HierarchicalDataTemplate 
                DataType="{x:Type r:NetworkViewModel}" 
                ItemsSource="{Binding Children}"
                >
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding NetworkIP}" Width="110" >
                             <TextBlock.InputBindings>
                                    <MouseBinding MouseAction="LeftClick"
                                    Command="{Binding DataContext.SelectItem, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"
                                    CommandParameter="{Binding}" />
                             </TextBlock.InputBindings>
                        </TextBlock>

                    </StackPanel>
                </HierarchicalDataTemplate>

How can this be done using a Style block in the Resources ? 如何使用ResourcesStyle块来完成此操作? The goal being to use the same style for all TextBoxes in the TreeView . 目标是对TreeView所有TextBoxes使用相同的样式。 Something that would sit in the Usercontrol.Resources and be refrenced by the HierarchicalDataTemplate . 放在Usercontrol.Resources中的东西,会被HierarchicalDataTemplate引用。

If I understand you correctly, you could define a template in the controls or windows resources with a target type (opposed to key x:Key=... ) to have it automatically applied to all items in the tree view. 如果我理解正确,则可以在控件或Windows资源中定义一个目标类型的模板(与键x:Key=... ),以使其自动应用于树视图中的所有项目。

Here is a small example with a definition of a template in the window resources, which contains the InputBindings definition. 这是一个小示例,在窗口资源中包含模板的定义,其中包含InputBindings定义。 This template will be automatically applied to all objects of type ItemViewModel if no other template is explicitly defined by the ItemsControl or TreeView . 如果ItemsControlTreeView没有显式定义其他模板,则该模板将自动应用于ItemViewModel类型的所有对象。 In this example, the items are displayed in a simple ItemsControl but it works for the TreeView just the same. 在此示例中,项目显示在简单的ItemsControl但它对于TreeView工作原理相同。

Note that for this to work, all items in the TreeView need to be of the same type. 请注意,要使此功能起作用, TreeView所有项目都必须具有相同的类型。 It is not sufficient if they are derived from the same base type. 如果它们衍生自相同的基本类型是不够的。 The template will only be applied, if the type defined in Template.DataType is exactly the same as the type of the ViewModel . 仅当Template.DataType定义的类型与ViewModel的类型完全相同时,才应用Template.DataType If your TreeViews ItemsScource s contain mixed type, you would need to specify the template for every type separately. 如果TreeViews ItemsScource包含混合类型,则需要分别为每种类型指定模板。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:loc="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate DataType="{x:Type loc:ItemViewModel}">
            <TextBlock Text="{Binding Name}" Width="110" >
                <TextBlock.InputBindings>
                    <MouseBinding 
                        MouseAction="LeftClick"
                        Command="{Binding SelectItem}" />
                </TextBlock.InputBindings>
            </TextBlock>
        </DataTemplate>
    </Window.Resources>

    <Grid>
        <ItemsControl ItemsSource="{Binding Items}" />
    </Grid>
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM