none
Conditional Triggers

    Question

  • Is there a way to create a trigger like so:

    Code Snippet

    <Trigger Property="Opacity" Value="< .1">

      <Setter />

      ...

    </Trigger>

     

    A way to possibly use less than, greater than, etc. ?

    Monday, May 07, 2007 2:28 PM

Answers

  • DataTriggers allow you to specify a Binding with which you can specify the trigger's condition.  That Binding's Converter can be set to a value converter which reduces a range into a discrete set of values, as demonstrated here: http://www.codeproject.com/WPF/HiliteListViewItemsInWPF.asp   If you point a DataTrigger's Binding at the element with the Opacity property of interest (via the RelativeSource.Self), you can use that technique.

     

    For example:

    Code Snippet

    <Grid>

      <Grid.Resources>

          <local:RangeToBooleanConverter x:Key="conv" />

          <Style TargetType="{x:Type TextBlock}">

             <Style.Triggers>

               <DataTrigger

                  Binding="{Binding

                            Path=Opacity,

                            RelativeSource={RelativeSource Self},

                            Converter={StaticResource conv}}"

                  Value="True">

                <Setter Property="HorizontalAlignment" Value="Center" />

              </DataTrigger>

            </Style.Triggers>

          </Style>

        </Grid.Resources>

      <TextBlock x:Name="txt" Text="Hello" Background="Tan" Opacity="0.4" />

    </Grid>

     

    and the converter

     

    Code Snippet

    public class RangeToBooleanConverter : IValueConverter

    {

      public object Convert(object value, Type targetType,

        object parameter, CultureInfo culture)

      {

        return ((double)value) < 0.5;

      }

     

      public object ConvertBack(object value, Type targetType,

        object parameter, CultureInfo culture)

      {

        throw new Exception("Nope.");

      }

    }

     

    Monday, May 07, 2007 3:40 PM

All replies

  • DataTriggers allow you to specify a Binding with which you can specify the trigger's condition.  That Binding's Converter can be set to a value converter which reduces a range into a discrete set of values, as demonstrated here: http://www.codeproject.com/WPF/HiliteListViewItemsInWPF.asp   If you point a DataTrigger's Binding at the element with the Opacity property of interest (via the RelativeSource.Self), you can use that technique.

     

    For example:

    Code Snippet

    <Grid>

      <Grid.Resources>

          <local:RangeToBooleanConverter x:Key="conv" />

          <Style TargetType="{x:Type TextBlock}">

             <Style.Triggers>

               <DataTrigger

                  Binding="{Binding

                            Path=Opacity,

                            RelativeSource={RelativeSource Self},

                            Converter={StaticResource conv}}"

                  Value="True">

                <Setter Property="HorizontalAlignment" Value="Center" />

              </DataTrigger>

            </Style.Triggers>

          </Style>

        </Grid.Resources>

      <TextBlock x:Name="txt" Text="Hello" Background="Tan" Opacity="0.4" />

    </Grid>

     

    and the converter

     

    Code Snippet

    public class RangeToBooleanConverter : IValueConverter

    {

      public object Convert(object value, Type targetType,

        object parameter, CultureInfo culture)

      {

        return ((double)value) < 0.5;

      }

     

      public object ConvertBack(object value, Type targetType,

        object parameter, CultureInfo culture)

      {

        throw new Exception("Nope.");

      }

    }

     

    Monday, May 07, 2007 3:40 PM
  • Thanks, that solves my problem.
    Monday, May 07, 2007 4:56 PM