简体   繁体   English

CheckBox 在鼠标悬停和选中 state 时更改了背景

[英]CheckBox changed background on mouse over and in checked state

I'm writing template for CheckBox control.我正在为 CheckBox 控件编写模板。 Here is CheckBox template code:这是 CheckBox 模板代码:


<Style x:Key="{x:Type CheckBox}"
       TargetType="{x:Type CheckBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CheckBox}">
                    <BulletDecorator Background="Transparent">
                        <BulletDecorator.Bullet>
                            <Border x:Name="Border"
                                    Width="15"
                                    Height="15"
                                    CornerRadius="1"
                                    BorderThickness="1"
                                    BorderBrush="DarkGray"
                                    Background="White">
                            </Border>
                        </BulletDecorator.Bullet>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CheckStates">
                                <VisualState x:Name="Checked">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="{StaticResource DefaultButtonBlueBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                                       Storyboard.TargetProperty="BorderBrush">
                                            <DiscreteObjectKeyFrame KeyTime="0"
                                                                    Value="{StaticResource DefaultButtonBlueBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                           
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter Margin="4,0,0,0"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"
                            RecognizesAccessKey="True" />
                    </BulletDecorator>
            </Setter.Value>
        </Setter>
    </Style>

I want to change border background color when checkbox in checked state and mouse over.我想在选中 state 的复选框并将鼠标悬停时更改边框背景颜色。 I tried to specify MultiTrigger like that:我试图像这样指定 MultiTrigger:

<MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsChecked" Value="True" />
                                <Condition Property="IsMouseOver" Value="True" />
                            </MultiTrigger.Conditions>
                            <MultiTrigger.Setters>
                                <Setter TargetName="Border" Property="Background" Value="Red" />
                            </MultiTrigger.Setters>
                        </MultiTrigger>

But this don't work.但这不起作用。

Is this even possible in wpf?这在 wpf 中是否可能?

Thank you.谢谢你。

WPF (opposed to UWP) has stricter limitations regarding the possibilities when using the VisualStateManager . WPF(与 UWP 相对)对使用VisualStateManager时的可能性有更严格的限制。 You can only target a property once per state.每个 state 只能定位一个属性一次。 Which makes quite sense.这很有意义。 When in the Checked state, the property BorderBackground is animated to the corresponding value.当在Checked state 中时,属性BorderBackground被动画到相应的值。 When your MultiTrigger is activated it tries to modify the Border.Background property, which is currently blocked by the active animation.当您的MultiTrigger被激活时,它会尝试修改Border.Background属性,该属性当前被活动的 animation 阻止。

You can either replace the relevant visual states with triggers or let animation and trigger target different elements.您可以用触发器替换相关的视觉状态,也可以让 animation 和触发器针对不同的元素。

Simply overlay the content of the BulletDecorator with an additional Border and toggle its Background instead:只需用额外的Border覆盖BulletDecorator的内容并切换其Background即可:

<Style x:Key="{x:Type CheckBox}" TargetType="{x:Type CheckBox}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type CheckBox}">
        <BulletDecorator Background="Transparent">
          <BulletDecorator.Bullet>
            <Grid>
              <Border x:Name="MouseOverBorder" 
                      Panel.ZIndex="1" 
                      Background="Transparent" 
                      CornerRadius="1"
                      BorderThickness="1" />
              <Border x:Name="Border"
                      Width="15"
                      Height="15"
                      CornerRadius="1"
                      BorderThickness="1"
                      BorderBrush="DarkGray"
                      Background="White" />
            </Grid>
          </BulletDecorator.Bullet>

          <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="CheckStates">
              <VisualState x:Name="Checked">
                <Storyboard>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                 Storyboard.TargetProperty="Background">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource DefaultButtonBlueBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                  <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                 Storyboard.TargetProperty="BorderBrush">
                    <DiscreteObjectKeyFrame KeyTime="0"
                                            Value="{StaticResource DefaultButtonBlueBrush}" />
                  </ObjectAnimationUsingKeyFrames>
                </Storyboard>
              </VisualState>

            </VisualStateGroup>
          </VisualStateManager.VisualStateGroups>
          <ContentPresenter Margin="4,0,0,0"
                            VerticalAlignment="Center"
                            HorizontalAlignment="Left"
                            RecognizesAccessKey="True" />
        </BulletDecorator>

        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsChecked" Value="True" />
              <Condition Property="IsMouseOver" Value="True" />
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
              <Setter TargetName="MouseOverBorder" 
                      Property="Background" 
                      Value="Red" />
            </MultiTrigger.Setters>
          </MultiTrigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM