WPF Style DataTrigger with binding to DataContext not working

前端 未结 1 1319
你的背包
你的背包 2020-12-14 08:46

I have a TextBox with a style that has a DataTrigger which changes the text, like this:


    

        
相关标签:
1条回答
  • 2020-12-14 09:28

    Dependency Properties can be set from many different places; inline, animations, coercion, triggers, etc. As such a Dependency Property Value Precedence list was created and this dictates which changes override which other changes. Because of this order of precedence, we can't use a Trigger to update a property that is explicitly set inline in your XAML. Try this instead:

    <Grid>
        <TextBlock>
            <TextBlock.Style>
                <Style BasedOn="{StaticResource TextStyle}" TargetType="TextBlock">
                    <!-- define your default value here -->
                    <Setter Property="Text" Value="Foo" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding MyBool}" Value="True">
                            <!-- define your triggered value here -->
                            <Setter Property="Text" Value="Bar" />
                        </DataTrigger>
                     </Style.Triggers>
                 </Style>
             </TextBlock.Style>
         </TextBlock>
    </Grid>
    
    0 讨论(0)
提交回复
热议问题