如何设置 Silverlight ToggleButton 的默认 IsChecked 值
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我非常希望
TreeviewItem
类中的代码将根据自己的IsExpanded
属性的状态专门分配IsChecked
。尝试在您的样式中添加另一个 setter:-
默认情况下,这应该使 TreeViewItem 处于展开状态,并且可能会导致 TreeviewItem 同时将 ExpanderButton IsChecked 设置为 true。
I very much expect that the code in the
TreeviewItem
class will be specifically assigning a theIsChecked
depending on the state of is ownIsExpanded
property.Try adding another setter to your style:-
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.您也可以尝试以(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.