我有大量遗留的Styles.xaml文件,扩展器具有奇怪的默认行为。要展开,您只能点击小图标,而不是标题。
我似乎无法找到样式的任何问题,所以我的猜测是问题出在其他地方,有人可以确认吗?
以下是整个文件Pastebin,这是有趣的部分:
<!-- Expander -->
<ControlTemplate x:Key="ExpanderToggleButton" TargetType="{x:Type ToggleButton}">
<Border Name="Border" CornerRadius="0,0,0,0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0,0,1,0">
<Path Name="Arrow" Fill="#FF4682B4" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="ToggleButton.IsMouseOver" Value="true">
<Setter Property="Background" TargetName="Border" Value="{x:Null}"/>
<Setter Property="Fill" TargetName="Arrow" Value="#FF060606"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="Border" Value="{x:Null}"/>
<Setter Property="Fill" TargetName="Arrow" Value="#FF093E6A"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="Arrow" Property="Data" Value="M 0 4 L 4 0 L 8 4 Z" />
<Setter Property="Fill" TargetName="Arrow" Value="#FF093E6A"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
<Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" />
<Setter Property="Background" TargetName="Border" Value="{x:Null}"/>
<Setter Property="BorderBrush" TargetName="Border" Value="{x:Null}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="{x:Type Expander}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Name="ContentRow" Height="0"/>
</Grid.RowDefinitions>
<Border Name="Border" Grid.Row="0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="1" CornerRadius="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ToggleButton IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" Background="#FF4682B4" />
<ContentPresenter Grid.Column="1" Margin="4" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
<Border Name="Content" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="1,0,1,1" CornerRadius="0,0,2,2">
<ContentPresenter Margin="4" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="True">
<Setter TargetName="ContentRow" Property="Height" Value="{Binding ElementName=Content,Path=DesiredHeight}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
<Setter Property="Background" TargetName="Border" Value="{DynamicResource DisabledBorderBrush}"/>
<Setter Property="BorderBrush" TargetName="Border" Value="{DynamicResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:5)
模板没有问题。它可以像您期望的那样工作。您的ToggleButton.IsChecked
绑定到Expander.IsExpanded
媒体资源,且只有当此按钮发生更改时,IsChecked
状态Expander
才会打开/关闭。
如果您想要更改该行为,则必须将整个标头ContentPresenter
设为ToggleButton.Content
。像这样:
<ControlTemplate x:Key="ExpanderToggleButton" TargetType="{x:Type ToggleButton}">
<Border Name="Border" CornerRadius="0,0,0,0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="0,0,1,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Path Name="Arrow" Fill="#FF4682B4" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
<ContentPresenter Grid.Column="1"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
...
</ControlTemplate.Triggers>
</ControlTemplate>
<Style TargetType="{x:Type Expander}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Name="ContentRow" Height="0"/>
</Grid.RowDefinitions>
<Border Name="Border" Grid.Row="0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="1" CornerRadius="0,0,0,0">
<ToggleButton IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" Background="#FF4682B4">
<ToggleButton.Content>
<ContentPresenter ContentSource="Header" RecognizesAccessKey="True" />
</ToggleButton.Content>
</ToggleButton>
</Border>
<Border Name="Content" Grid.Row="1" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="1,0,1,1" CornerRadius="0,0,2,2">
<ContentPresenter Margin="4" />
</Border>
</Grid>
<ControlTemplate.Triggers>
....
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这样整个标题变为活动ToggleButton
,您可以使用整个标题打开/关闭
答案 1 :(得分:0)
这是Expander标题的模板:
<Border Name="Border" Grid.Row="0" Background="{x:Null}" BorderBrush="{x:Null}" BorderThickness="1" CornerRadius="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ToggleButton IsChecked="{Binding Path=IsExpanded,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}" OverridesDefaultStyle="True" Template="{StaticResource ExpanderToggleButton}" Background="#FF4682B4" />
<ContentPresenter Grid.Column="1" Margin="4" ContentSource="Header" RecognizesAccessKey="True" />
</Grid>
</Border>
正如您所看到的,有一个ToggleButton
绑定到IsExpanded
属性,我猜这是属于'怪异行为'