Question

How can I disable a Border control when its MouseDown event is triggered. I have the following XAML, but the Property Setter is not allowed in EventTrigger and I am constrained to do this only in XAML:

<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>
Was it helpful?

Solution

Try this:

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

We use a Storyboard to work around not being able to set a Setter directly and have the Animation instant for an immediate effect.

Update:

Download Link: Here

Another Update (To show a different state for disabled on the 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top