在其MouseDown EventTrigger中禁用WPF边框

时间:2013-06-06 08:53:06

标签: .net wpf xaml border eventtrigger

如何在触发MouseDown事件时禁用边框控件。我有以下XAML,但Property Setter在<{1}}中允许 ,我只能在XAML中执行此操作:

EventTrigger

1 个答案:

答案 0 :(得分:1)

试试这个:

<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>

我们使用Storyboard来解决无法直接设置Setter并让动画瞬间立即生效的问题。

<强>更新

下载链接:Here

另一次更新(在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>