1

I have many components in my XAML, I need fire Trigger when one or many components has your value changed.

I not want use one PropertyChangedTrigger for each components, I want use one Trigger for all components.

thanks.

1 Answers1

0

Yes, use a MultiDataTrigger. Here's an example from the linked MSDN documentation about how you can use this on multiple properties:

<Window.Resources>
  <c:Places x:Key="PlacesData"/>

  <Style TargetType="ListBoxItem">
    <Style.Triggers>
      <DataTrigger Binding="{Binding Path=State}" Value="WA">
        <Setter Property="Foreground" Value="Red" />
      </DataTrigger>    
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding Path=Name}" Value="Portland" />
          <Condition Binding="{Binding Path=State}" Value="OR" />
        </MultiDataTrigger.Conditions>
        <Setter Property="Background" Value="Cyan" />
      </MultiDataTrigger>
    </Style.Triggers>
  </Style>

  <DataTemplate DataType="{x:Type c:Place}">
    <Canvas Width="160" Height="20">
      <TextBlock FontSize="12"
           Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
      <TextBlock FontSize="12" Width="30"
                 Canvas.Left="130" Text="{Binding Path=State}"/>
    </Canvas>
  </DataTemplate>
</Window.Resources>

<StackPanel>
  <TextBlock FontSize="18" Margin="5" FontWeight="Bold"
    HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
  <ListBox Width="180" HorizontalAlignment="Center" Background="Honeydew"
    ItemsSource="{Binding Source={StaticResource PlacesData}}"/>
</StackPanel>

EDIT: Not the cleanest solution ever, but you could do something like this. Basically use the MultiDataTrigger to execute whenever any of the properties change. Then, you use a converter for a simple null check (or perhaps you could always return true in your case). That way, your value in the MultiDataTrigger is just True instead of a specific value.

Community
  • 1
  • 1
rmc00
  • 877
  • 10
  • 20
  • Thanks, but I want fire trigger to any value has changed, in your example trigger is fired when the conditional equals true, right? – Guilherme Sanches Sep 26 '16 at 15:06
  • Hmmm MultiTrigger and MultiDataTrigger need specific values because they do an equals comparison on reference values. Can you tell me more specifically what you're trying to solve? Are you using multiple properties to calculate a value or something? – rmc00 Sep 26 '16 at 15:17
  • Yes, I need fire one command in my viewmodel to populate a listbox when one or more textbox has change – Guilherme Sanches Sep 26 '16 at 17:33