开发者

How to set default IsChecked value for Silverlight ToggleButton

开发者 https://www.devze.com 2023-02-20 15:18 出处:网络
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.

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:TreeVi开发者_C百科ewItem">
    <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>


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号