如何设置 Silverlight ToggleButton 的默认 IsChecked 值

发布于 2024-10-28 16:11:25 字数 2492 浏览 2 评论 0原文

我有一个在 Silverlight TreeviewItem 样式中定义的切换按钮,我希望它以 IsChecked=true 状态开始。我尝试简单地设置 IsChecked=True,但这没有效果。

谢谢

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2024-11-04 16:11:25

我非常希望 TreeviewItem 类中的代码将根据自己的 IsExpanded 属性的状态专门分配 IsChecked

尝试在您的样式中添加另一个 setter:-

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

默认情况下,这应该使 TreeViewItem 处于展开状态,并且可能会导致 TreeviewItem 同时将 ExpanderButton IsChecked 设置为 true。

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.

早茶月光 2024-11-04 16:11:25

您也可以尝试以(n隐式)样式设置它,但我怀疑可能会发生某些事情(TreeView?)在运行时重置它,否则您的设置不应该无效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文