【问题标题】:Focus on Label, TextBlock and Border专注于Label、TextBlock和Border
【发布时间】:2010-07-28 09:26:14
【问题描述】:

我想创建一个带有圆角右上角和右下角的平面按钮。此按钮需要在单击和鼠标悬停时更改背景。

目前我的标记如下所示:

    <Border x:Name="MyButton" Height="25" Margin="0,5,0,0" CornerRadius="0 5 5 0" BorderThickness="1" BorderBrush="Gray" Style="{StaticResource myStyle1}">
        <StackPanel Orientation="Horizontal" Margin="8,0,0,0">
            <Image Source="image.jpg" Height="20"/>
            <TextBlock Text="My Button"/> <!-- Could also be a label if needs to be. -->
        </StackPanel>
    </Border>

    <Style x:Key="myStyle1" TargetType="{x:Type Border}">
        <Setter Property="Background" Value="{StaticResource MainContentForegroundColor}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>

鼠标悬停在触发器上效果很好,但我无法让点击触发器工作...我尝试过IsKeyboardFocusWithinIsFocused,但没有成功。

【问题讨论】:

    标签: wpf templates focus styles


    【解决方案1】:

    我认为你应该使用ToggleButton 而不是普通按钮-

    <ToggleButton
        Background="Transparent">
        <ToggleButton.Template>
            <ControlTemplate
                TargetType="{x:Type ToggleButton}">
                <Border
                    x:Name="MyButton"
                    Height="25"
                    Margin="0,5,0,0"
                    CornerRadius="0 5 5 0"
                    BorderThickness="1"
                    BorderBrush="Gray">
                    <StackPanel
                        Orientation="Horizontal"
                        Margin="8,0,0,0">
                        <Image
                            Source="image.jpg"
                            Height="20" />
                        <TextBlock
                            Text="My Button" /> 
                       <!-- Could also be a label if needs to be. -->
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger
                        Property="IsChecked"
                        Value="True">
                        <Setter
                            Property="Background"
                            Value="Red" />
                    </Trigger>
                    <Trigger
                        Property="IsMouseOver"
                        Value="True">
                        <Setter
                            Property="Background"
                            Value="Red" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </ToggleButton.Template>
    </ToggleButton>
    

    【讨论】:

      【解决方案2】:

      你可以创建一个 Button 样式,那么只有你会 IsPressed 属性。使用 VSM 查看下面的代码。

      <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type Button}">
                          <Grid>
                              <VisualStateManager.VisualStateGroups>
                                  <VisualStateGroup x:Name="CommonStates">
                                      <VisualState x:Name="Normal"/>
                                      <VisualState x:Name="MouseOver">
                                          <Storyboard>
                                              <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                                  <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF760D0D"/>
                                              </ColorAnimationUsingKeyFrames>
                                          </Storyboard>
                                      </VisualState>
                                      <VisualState x:Name="Pressed">
                                          <Storyboard>
                                              <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="border" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)">
                                                  <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF675A88"/>
                                              </ColorAnimationUsingKeyFrames>
                                          </Storyboard>
                                      </VisualState>
                                  </VisualStateGroup>
                              </VisualStateManager.VisualStateGroups>
                              <Border x:Name="border" BorderBrush="#FF5A8876" BorderThickness="3" Background="#FFF4EDED"/>
                              <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                          </Grid>
                      </ControlTemplate>
                  </Setter.Value>
              </Setter>
          </Style>
      
      
      <Button HorizontalAlignment="Left" Style="{DynamicResource ButtonStyle1}" VerticalAlignment="Top" Width="180" Height="61" Content="Button"/>
      

      【讨论】:

      • 我有另一个按钮模板,现在我想为这个特殊按钮创建一个用户控件,我可以在其中设置“模式”属性,它会适当地改变样式。我该怎么做?例如。 ...
      • 否 只需为新模板创建一个名为“ButtonStyle2”的新按钮样式,并使用 Style="{DynamicResource ButtonStyle2}" 进行引用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      相关资源
      最近更新 更多