具有自定义样式的WPF TreeView

WPF TreeView with custom style

你可以帮帮我吗? 我需要在每个项目都带有一个CheckBox的TreeView。 我不明白,我是这样开始的,似乎是绝对错误的方式:

1
2
3
4
5
6
7
8
9
10
11
   <TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <!-- <Setter Property="?????"> WHAT SHOULD BE HERE?
                    <Setter.Value>

                    </Setter.Value>
                </Setter> -->
            </Style>
        </TreeView.Resources>
    </TreeView>

给我看看 一些简单的例子

UPD:哦...我想我需要一个ControlTemplate,但我仍然不知道如何制作它

UPD2:天哪,我正在加深对此的迷惑。 我应该在这里的某个地方使用RelativeSource标记扩展吗? 谁来帮帮我!

UPD3:现在,它不像TreeBox那样工作-我无法展开\折叠项目,尽管我稍微向前移动了-我可以看到复选框。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
      <TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
        <TreeView.Resources>
            <Style x:Key="{x:Type TreeViewItem}" TargetType="TreeViewItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Grid Margin="2">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <StackPanel Grid.Row="0"  Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding Path=IsActive}"/>
                                    <TextBlock Text="{Binding Path=Alias.UserName}"/>
                                </StackPanel>
                                <ItemsPresenter Grid.Row="1"/>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

        </TreeView.Resources>
    </TreeView>

像这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<TreeView Grid.Row="0" Grid.Column="0" Name="StagesTreeView" Margin="5">
    <TreeView.Resources>
        <Style TargetType="TreeViewItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TreeViewItem">
                        <StackPanel Orientation="Horizontal">
                            <CheckBox Margin="2" Name="checkBox1"/>
                            <Border Padding="2">
                                <ContentPresenter Name="PART_header" ContentSource="Header"/>
                            </Border>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TreeView.Resources>
</TreeView>


您可能会发现这很有用:在WPF TreeView中使用复选框