代码之家  ›  专栏  ›  技术社区  ›  testalino

WPF扩展器-修改样式

  •  0
  • testalino  · 技术社区  · 14 年前

    我使用Expression Blend创建了一个模板,使用“Edit a copy…”。这给了我大量的XAML语句。我继续编辑那些声明,但没有得到我想要的效果。

    我想 修改扩展器的上下状态

    以下是我从Expression Blend得到的信息:

    <Window.Resources>
        <Style x:Key="ExpanderDownHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid SnapsToDevicePixels="False" Height="25">
                                <Grid.Background>
                                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                        <GradientStop Color="#FFEFEFEF" Offset="0.326"/>
                                        <GradientStop Color="#FF9F9F9F" Offset="1"/>
                                    </LinearGradientBrush>
                                </Grid.Background>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="40"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Ellipse x:Name="circle" HorizontalAlignment="Center" Height="19" Stroke="DarkGray" VerticalAlignment="Center" Width="19"/>
                                <Path x:Name="arrow" Data="M 0,2 H 10,10"  HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="#666" StrokeThickness="2" VerticalAlignment="Center">
    
                                </Path>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
    
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
                                <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderUpHeaderStyle" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}">
                        <Border Padding="{TemplateBinding Padding}">
                            <Grid SnapsToDevicePixels="False" Height="25" Background="Transparent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="40"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Ellipse x:Name="circle" HorizontalAlignment="Center" Height="19" Stroke="DarkGray" VerticalAlignment="Center" Width="19"/>
                                <Path x:Name="arrow" Data="M 0,5 H 10,10 M 5,0 L 5,10"  HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="#666" StrokeThickness="2" VerticalAlignment="Center">
                                </Path>
                                <ContentPresenter Grid.Column="1" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="True" VerticalAlignment="Center"/>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
    
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="#FF3C7FB1"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="#222"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Stroke" TargetName="circle" Value="#FF526C7B"/>
                                <Setter Property="StrokeThickness" TargetName="circle" Value="1.5"/>
                                <Setter Property="Stroke" TargetName="arrow" Value="#FF003366"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="ExpanderTemplate2" TargetType="{x:Type Expander}">
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    
           <Setter Property="Background" Value="Transparent"/>
           <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
           <Setter Property="VerticalContentAlignment" Value="Stretch"/>
           <Setter Property="BorderBrush" Value="Black"/>
           <Setter Property="BorderThickness" Value="1"/>
           <Setter Property="Padding" Value="0,0,0,0"/>
           <Setter Property="Template">
    <Setter.Value>
     <ControlTemplate TargetType="{x:Type Expander}">
      <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="3" SnapsToDevicePixels="true">
       <DockPanel>
        <ToggleButton x:Name="HeaderSite" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" MinWidth="0" MinHeight="0" Padding="{TemplateBinding Padding}" Style="{StaticResource ExpanderDownHeaderStyle}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
        <ContentPresenter x:Name="ExpandSite" DockPanel.Dock="Bottom" Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
       </DockPanel>
      </Border>
      <ControlTemplate.Triggers>
       <Trigger Property="IsExpanded" Value="true">
        <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
       </Trigger>
       <Trigger Property="ExpandDirection" Value="Up">
        <Setter Property="DockPanel.Dock" TargetName="ExpandSite" Value="Top"/>
        <Setter Property="DockPanel.Dock" TargetName="HeaderSite" Value="Bottom"/>
        <Setter Property="Style" TargetName="HeaderSite" Value="{StaticResource ExpanderUpHeaderStyle}"/>
       </Trigger>
       <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
       </Trigger>
      </ControlTemplate.Triggers>
     </ControlTemplate>
    </Setter.Value>
    

    我修改了路径值并删除了一些内容。我还尝试为这两种状态(一种是渐变,一种是透明背景)获取不同的背景。

    有什么建议我做错了,或者这应该怎么做?

    3 回复  |  直到 14 年前
        1
  •  0
  •   Rachel    14 年前

    你需要在触发器中设置模板。

    <ControlTemplate.Triggers>
       <!-- This is your Trigger Condition -->
       <Trigger Property="IsExpanded" Value="true">
          <!-- This is what happens when condition is met - Right now it is just showing the ExpandSite -->
          <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
    
          <!--What is should do is modify the Template of the control to use a different one
              Not really sure what part of the control you want to modify, but this is the general idea -->
          <Setter Property="Template" Value="..." TargetName="..." />
       </Trigger>
    </ControlTemplate.Triggers>
    
        2
  •  0
  •   testalino    14 年前

    例如,我想改变上下切换按钮的样式。我为up和down创建了一个单独的样式,并在该样式中创建了修改按钮外观的触发器。

    <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButton">
        <Path Name="Triangle" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 10 10 L 20 0 Z" Fill="Black" />
        <ControlTemplate.Triggers>
            <Trigger Property="IsChecked" Value="true">
                <Setter TargetName="Triangle" Property="Data" Value="M 0 10 L 10 0 L 20 10 Z" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    我对其他想要改变的财产也做了同样的事。

        3
  •  0
  •   Simo    6 年前
    ExpanderDownHeaderStyle
    

    自定义箭头顶部或按钮修改路径

    上箭头路径:

    Data="M 0 0 L 10 10 L 20 0" 
    

    Data="M 20 12 L 10 0 L 0 12"