1

Using an event trigger for some Storyboards.
Need to also assign Opacity to TextBlock with no Storyboard.
How do I directly assign a value based on a trigger?

<Expander.Triggers>
   <EventTrigger RoutedEvent="Expander.Expanded">

Tried doing this in an Expanded event but that did not work.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • possible duplicate of [Setting a property with an EventTrigger](http://stackoverflow.com/questions/942548/setting-a-property-with-an-eventtrigger) – Abe Heidebrecht Jul 08 '13 at 16:10
  • That answer says use Attached Behaviors. It is not readily apparent to me how to do this with an AttachedBehavior. I don't have Expression Blend. – paparazzo Jul 08 '13 at 16:19
  • Attached behaviors do not require blend. They are part of the blend sdk (and since blend is shipped with vs 2012, it is shipped with that as well). And also, blend behaviors are a formalization of the attached behavior through dependency properties. Here is a good primer on the differences: http://briannoyes.net/2012/12/20/AttachedBehaviorsVsAttachedPropertiesVsBlendBehaviors.aspx – Abe Heidebrecht Jul 08 '13 at 16:48
  • @AbeHeidebrecht I am aware that Attached Behaviors do not require Blend. It is still not apparent to me how to do this with an Attached Behavior. – paparazzo Jul 08 '13 at 17:48
  • Why not `Storyboard`? The usual `EventTrigger` requires a `Storyboard`. – Anatoliy Nikolaev Jul 08 '13 at 18:31
  • @AnatoliyNikolaev See question. I have a property I want to assign directly with no storyboard. – paparazzo Jul 08 '13 at 18:41
  • I think you can use `DataTrigger` with the `IsExpanded` property. And all of this is to define a `DataTemplate` to trigger could access `TextBlock`. – Anatoliy Nikolaev Jul 08 '13 at 18:45
  • @AnatoliyNikolaev I tried that but cannot figure out how to access the IsExpanded for the Expander. IsExpanded is not really "data". – paparazzo Jul 08 '13 at 19:07

1 Answers1

1

Here is a small example of the DataTemplate. There is a Grid in which there are Expander with two TextBlocks. One is inside the expander InnerTextBlock, and the other one is not in - OuterTextBlock. When a property IsExpanded == True we do specific operations with DataTriggers.

XAML

<Window.Resources>
    <DataTemplate DataType="{x:Type local:MyExpanderData}">
        <Grid x:Name="MainGrid">
            <Expander Name="MyExpander" Header="{Binding HeaderName}" IsExpanded="{Binding isExpanded}">
                <TextBlock Name="InnerTextBlock" Text="InnerTextBlock" Width="150" Height="30" VerticalAlignment="Top" FontSize="16" />
            </Expander>

            <TextBlock Name="OuterTextBlock" Text="OuterTextBlock" Width="150" Height="30" Margin="0,30,0,0" FontSize="16" />
        </Grid>

        <!-- Our DataTriggers -->
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding ElementName=MyExpander, Path=IsExpanded}" Value="True">
                <Setter TargetName="OuterTextBlock" Property="Opacity" Value="0.5" />                    
            </DataTrigger>

            <DataTrigger Binding="{Binding ElementName=MyExpander, Path=IsExpanded}" Value="True">
                <Setter TargetName="InnerTextBlock" Property="Foreground" Value="Red" />
            </DataTrigger>

            <DataTrigger Binding="{Binding ElementName=MyExpander, Path=IsExpanded}" Value="True">
                <Setter TargetName="MyExpander" Property="Header" Value="Expander open" />
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

    <!-- Resource data for the Expander -->
    <local:MyExpanderData x:Key="MyExpanderData" HeaderName="Test Expander" isExpanded="True" />
</Window.Resources>

<Grid>
    <!-- Our DataTemplate in ContentControl -->
    <ContentControl Name="MyContentControl" Content="{StaticResource MyExpanderData}" />       
</Grid>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

public class MyExpanderData
{
    private bool expanded = false;
    private string headerName = "";

    public bool isExpanded
    {
        get
        {
            return expanded;
        }

        set
        {
            expanded = value;
        }
    }

    public string HeaderName
    {
        get
        {
            return headerName;
        }

        set
        {
            headerName = value;
        }
    }
}

In the class MyExpanderData stored data, which are then Binding in our DataTemplate. All properties are stored in a DataTemplate, we can set via this class.

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68