为什么两个数据触发器不起作用,仅最后一个不起作用?

时间:2019-08-08 11:47:01

标签: c# wpf xaml

如何正确解决两个DataTrigger的错误

<Page.Resources>
    <Storyboard x:Key="OpenMenu">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)" >
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="250"/>
        </DoubleAnimationUsingKeyFrames>

    </Storyboard>
    <Storyboard x:Key="CloseMenu">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)" >
            <EasingDoubleKeyFrame KeyTime="0" Value="250"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>

    <Style TargetType="TextBox" x:Key="companyStyle" BasedOn="{StaticResource MaterialDesignFloatingHintTextBox}" >        
        <Style.Triggers>               

            <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=comboBoxRole}" Value="AppDeveloper">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource OpenMenu}"/>
                </DataTrigger.EnterActions>
            </DataTrigger>

            <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=comboBoxRole}" Value="EndUser">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource CloseMenu}"/>
                </DataTrigger.EnterActions>
            </DataTrigger>                

        </Style.Triggers>
    </Style>        

</Page.Resources>

在这里我针对不同情况调用相同的属性(如果值-开发人员,请致电open;如果值- user ,请致电close)。当我在combobox中选择值时,我想显示或隐藏开发人员字段。

一个更好的版本,如果不透明度为0,则更改属性visability= collapse

对不起,也许这是一个愚蠢的问题,但是找不到正确编写此逻辑的方法。正确或错误对我没有帮助,因为我可以添加更多组合框项目和字段

我的组合框

<ComboBox x:Name="comboBoxRole" 
                  SelectedItem="{Binding Role}" 
                  ItemsSource="{Binding RolesEnum}" 
                  materialDesign:HintAssist.Hint="Choose the role" 
                  Foreground="RoyalBlue"
                  FontWeight="Heavy"
                  Style="{StaticResource MaterialDesignFloatingHintComboBox}" 
                  VerticalAlignment="Top"
                  Margin="0,90,0,0"
                  Width="150"                      
                  />

,例如一些开发人员的字段

<!-- Company adress -->
                <TextBox x:Name="companyAdress" materialDesign:HintAssist.Hint="Company adress" Style="{StaticResource companyStyle}" Margin="0,0,0,20" FontSize="18" Foreground="RoyalBlue" FontWeight="Heavy">
                    <TextBox.Text>
                        <Binding  Mode="TwoWay" Path="CompanyAdress"  UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

                <!-- Company website -->
                <TextBox x:Name="companyWebsite" materialDesign:HintAssist.Hint="Company Website" Style="{StaticResource companyStyle}" Margin="0,0,0,20" FontSize="18" Foreground="RoyalBlue" FontWeight="Heavy">
                    <TextBox.Text>
                        <Binding  Mode="TwoWay" Path="CompanyWebsite"  UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>

1 个答案:

答案 0 :(得分:1)

两个Storyboards的定义完全相同。无论如何,您可以使用ExitAction删除Storyboard。此示例有效:

<Window ...>
    <Window.Resources>
        <Storyboard x:Key="OpenMenu">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
            </DoubleAnimationUsingKeyFrames>

        </Storyboard>
        <Storyboard x:Key="CloseMenu">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Opacity)" >
                <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

        <Style TargetType="TextBox" x:Key="companyStyle" >
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=comboBoxRole}" Value="AppDeveloper">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Name="sb" Storyboard="{StaticResource OpenMenu}"/>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <RemoveStoryboard BeginStoryboardName="sb" />
                    </DataTrigger.ExitActions>
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=SelectedValue, ElementName=comboBoxRole}" Value="EndUser">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard Name="sb2" Storyboard="{StaticResource CloseMenu}"/>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <RemoveStoryboard BeginStoryboardName="sb2" />
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel Margin="10">
        <TextBox Style="{StaticResource companyStyle}" />
        <ComboBox x:Name="comboBoxRole" SelectedValuePath="Content">
            <ComboBoxItem>AppDeveloper</ComboBoxItem>
            <ComboBoxItem>EndUser</ComboBoxItem>
        </ComboBox>
    </StackPanel>
</Window>