1
votes

I have a Toggle button defined in a style for a Silverlight TreeviewItem, and I would like it to start off in the IsChecked=true state. I tried simply setting IsChecked=True, but that has no effect.

Thanks

Example XAML:

<Style x:Name="CheckedToggleButton"
       TargetType="ToggleButton">
    <Setter Property="IsChecked"
            Value="True" />
    <Setter Property="Margin"
            Value="0" />
</Style>

<Style x:Key="TreeViewItemStyle"
       TargetType="controls:TreeViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:TreeViewItem">
                <Grid Margin="2">
                    <Grid.RowDefinitions>
                        <!--ContentPresenter Row-->
                        <RowDefinition Height="Auto" />
                        <!--ExpanderButton Row-->
                        <RowDefinition Height="Auto" />
                        <!--ItemsPresenter Row-->
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <ContentPresenter Cursor="{TemplateBinding Cursor}"
                                      HorizontalAlignment="Stretch"
                                      Margin="{TemplateBinding Padding}"
                                      x:Name="content"
                                      Content="{TemplateBinding Header}"
                                      ContentTemplate="{TemplateBinding HeaderTemplate}"
                                      Grid.RowSpan="1"/>
                    <ToggleButton x:Name="ExpanderButton"
                                  Style="{StaticResource CheckedToggleButton}"
                                  IsChecked="True"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Bottom"
                                  Width="15"
                                  Height="15"
                                  Grid.Row="1" />
                    <ItemsPresenter x:Name="ItemsHost"
                                    Visibility="{Binding ElementName=ExpanderButton, Path=IsChecked, Converter={StaticResource boolviz}}"
                                    Grid.Row="2"
                                    HorizontalAlignment="Stretch" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
2

2 Answers

1
votes

I very much expect that the code in the TreeviewItem class will be specifically assigning a the IsChecked depending on the state of is own IsExpanded property.

Try adding another setter to your style:-

 <Setter Property="IsExpanded" Value="True" />

This should put the TreeViewItem in an expanded state by default and will likely cause the TreeviewItem to set the ExpanderButton IsChecked to true at the same time.

0
votes

You can also try to set it in a(n implicit) style, but I have the suspicion that it might happen that something (the TreeView?) resets it runtime, otherwise it shouldn't happen that your setting has no effect.