views:

3143

answers:

2

Could you help me? I need a TreeView with a CheckBox at every item. 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

UPD2: Gosh, as much I'm deepening into this I'm getting bewildered. Should I use RelativeSource markup extension somewhere here? 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.

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

You might find this useful: Working with Checkboxes in the WPF TreeView

amarcy
Looks like ideal for me. Hold on, I'm gonna scrutinize it. Thank you!
Ike
I can't sort that out. What a lousy programmer I am. These WPF things difficult and way too tangled for me right now.
Ike
+1  A: 

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>
kek444
What ContentPresenter for? I'm still not getting...
Ike
The ContentPresenter is a Frameworkelement which represents the placeholder for presenting the actual data of a TreeViewItem (and many other controls). All the other controls which are part of a template are there to create a cretain visual environment, like borders, grids, etc., but the ContentPresenter is where the actual data item will go when the TreeViewItem tries to render it.
kek444