简体   繁体   English

具有自定义样式的WPF TreeView

[英]WPF TreeView with custom style

Could you help me? 你可以帮帮我吗? I need a TreeView with a CheckBox at every item. 我需要在每个项目上都带有一个CheckBox的TreeView。 I can't get it, I started like this, and seems like absolutely wrong way: 我不明白,我是这样开始的,似乎是绝对错误的方式:

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

Show me pls. 给我看看 some simple example 一些简单的例子

UPD: Oh... I think I need a ControlTemplate, but I still dunno how to make it UPD:哦...我想我需要一个ControlTemplate,但我仍然不知道如何制作它

UPD2: Gosh, as much I'm deepening into this I'm getting bewildered. UPD2:天哪,我正在加深对此的迷惑。 Should I use RelativeSource markup extension somewhere here? 我应该在这里的某个地方使用RelativeSource标记扩展吗? Someone help me! 谁来帮帮我!

UPD3: Now it doesn't work like a TreeBox - I can't expand\\collapse items, although I slightly moved forward - I can see the checkboxes. UPD3:现在,它不能像TreeBox一样工作-我无法展开\\折叠项目,尽管我稍微向前移动了-我可以看到复选框。

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

Something like this: 像这样:

<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中使用复选框

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

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