1

对某些 Storyboard 使用事件触发器。
还需要将不透明度分配给没有情节提要的 TextBlock。
如何根据触发器直接分配值?

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

尝试在扩展事件中执行此操作,但没有成功。

4

1 回答 1

1

下面是一个小例子DataTemplate。有一个Grid其中有Expander两个TextBlocks。一个在扩展器内InnerTextBlock,另一个不在 - 中OuterTextBlock。当一个属性IsExpanded==True我们用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;
        }
    }
}

在类MyExpanderData中存储数据,然后Binding在我们的DataTemplate. 所有属性都存储在一个 中DataTemplate,我们可以通过这个类进行设置。

于 2013-07-08T19:44:13.323 回答