0

如何在触发 MouseDown 事件时禁用边框控件。我有以下内容,XAML不允许使用,并且我只能在 XAML 中执行此操作:Property SetterEventTrigger

<Border x:Name="border">
    <Border.Style>
        <Style TargetType={x:Type Border}>
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseDown">
                    <EventTrigger.Actions>
                        <!--Setters not allowed in EventTriggers-->
                        <Setter Property="IsEnabled" Value="False"/> 
                    </EventTrigger.Actions>
                </EventTrigger>
            </Style.Triggers?
        </Style>
    </Border.Style>
</Border>
4

1 回答 1

2

尝试这个:

<Border x:Name="border">
  <Border.Style>
    <Style TargetType="{x:Type Border}">
      <Style.Triggers>
        <EventTrigger RoutedEvent="MouseDown">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Duration="0"
                                                Storyboard.TargetProperty="(Border.IsEnabled)">
                  <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                      <sys:Boolean>False</sys:Boolean>
                    </DiscreteObjectKeyFrame.Value>
                  </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
      </Style.Triggers>
    </Style>
  </Border.Style>
</Border>

我们使用 aStoryboard来解决无法Setter直接设置 a 的问题,并让 Animation 瞬间产生即时效果。

更新:

下载链接:这里

另一个更新(在 上显示不同的禁用状态Border

<Border x:Name="border"
        BorderThickness="5">
  <Border.Style>
    <Style TargetType="{x:Type Border}">
      <Setter Property="Background"
            Value="BurlyWood" />
      <Setter Property="BorderBrush"
            Value="Blue" />
      <Style.Triggers>
        <EventTrigger RoutedEvent="MouseDown">
          <EventTrigger.Actions>
            <BeginStoryboard>
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Duration="0"
                                                Storyboard.TargetProperty="(Border.IsEnabled)">
                  <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                      <sys:Boolean>False</sys:Boolean>
                    </DiscreteObjectKeyFrame.Value>
                  </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger.Actions>
        </EventTrigger>
        <Trigger Property="IsEnabled"
                  Value="False">
          <Setter Property="Background"
                  Value="Gray" />
          <Setter Property="BorderBrush"
                  Value="DarkGray" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </Border.Style>
  <Button Margin="100"
          Content="Some Button" />
</Border>
于 2013-06-06T09:56:22.420 回答