Good day! Help please add the trigger. This trigger changes the component's Opacity (StackPanel) from 0 to 70% (that is, makes the component visible) when you click on it with the left mouse button.

I need to modify the trigger so that the next time I press the left mouse button , the Opacity again goes to 0 (the component becomes invisible). That is, the trigger must catch 2 conditions:

  1. MouseLeftButtonDown event
  2. Property = "Opacity" Value = "0.7"

Can I make an EventTrigger with two conditions?

How to bind the MouseLeftButtonDown event in DataTrigger?

<Style x:Key="Pic_Panel_Visibility" TargetType="StackPanel"> <Setter Property="Visibility" Value="Visible"/> <Setter Property="Opacity" Value="0"/> <Style.Triggers> <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown"> <EventTrigger.Actions> <BeginStoryboard Name="BeginStory"> <Storyboard> <DoubleAnimation Duration="0:0:0.1" Storyboard.TargetProperty="(UIElement.Opacity)" From="0" To="0.7"/> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> <!-- Тут нужен описанный в вопросе функционал --> </Style.Triggers> </Style> 
  • Good question. When I ran into something like this about a year ago, I decided to implement it through the code-behind (I don’t see anything wrong and now I’m doing this — work only with the visual part). But it is curious to know the solution through triggers. - user227049
  • I suspect that only one markup can not cope, have to write code - Andrew NOP

1 answer 1

You can do without the code-behind, almost.

You need to make the attached property, the value of which will be set through the EventTrigger, in your case when you click the left mouse button.

And next to MultiTrigger, which will work, subject to the presence of a value in the above-mentioned property.

UPD: code.

Model:

 public class Model : INotifyPropertyChanged { private bool _isPropertySet; public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } public Model() { SetPropertyCommand = new DelegateCommand(o => { IsPropertySet = !IsPropertySet; }); } public bool IsPropertySet { get { return _isPropertySet; } set { _isPropertySet = value; OnPropertyChanged(nameof(IsPropertySet)); } } public ICommand SetPropertyCommand { get; set; } 

View:

 <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp1" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:Model /> </Window.DataContext> <Window.Resources> <ResourceDictionary> <Style TargetType="StackPanel"> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Setters> <Setter Property="Opacity" Value="0.1" /> </MultiDataTrigger.Setters> <MultiDataTrigger.Conditions> <Condition Binding="{Binding DataContext.IsPropertySet, RelativeSource={RelativeSource Self}}" Value="False" /> </MultiDataTrigger.Conditions> </MultiDataTrigger> </Style.Triggers> </Style> </ResourceDictionary> </Window.Resources> <Grid> <StackPanel Width="80" Height="80" Background="Red"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseLeftButtonDown"> <i:InvokeCommandAction Command="{Binding SetPropertyCommand}" /> </i:EventTrigger> </i:Interaction.Triggers> </StackPanel> </Grid> 

You need one trigger per event to set the property, and one trigger that binds to this property and sets the desired Opacity value.

  • Answer the code immediately. A better ready simple example that you can run and see. - Andrei NOP
  • I understand that on my computer, from which I answer, there is no studio yet. Tomorrow I will answer with examples - Anton Shakalo
  • Please write the code, I'm still a beginner and this kind of tangles are difficult for me. - Slava Donnikov 4:16 pm
  • Added sample code - Anton Shakalo